質問

I am building a webpage in CakePHP and jQuery that I would like to ajaxify, but also want to have it be accessible for everyone, so I'm digging into progressive enhancement. In beforeRender() I have this simple snippet that works awfully good for simple requests:

public function beforeRender()
{
    if($this->request->is('ajax'))
    {
        $this->layout = 'ajax';
    }
}

so when I have an ajax request it just renders the content and not the whole menu, header, etc, and when not, renders normally.

But I'm getting lost as how it should be the proper answer from server. I mean, if it's a normal request, page loads normally. But if it's an ajax request, I can't get any variable that I would set, for example, via javascript. Or any operation status (if he failed, was successful, etc). For now I'm sending anything extra via response headers, but I don't know if it's good practice to do so, so, any recommendations for how can I solve this?

正しい解決策はありません

他のヒント

You can use CakeResponse to debug.

if ($this->RequestHandler->isAjax()) {
//your code here
return new CakeResponse(array('body'=> json_encode('...message here...'),'status'=>200));
}

This is inside the ajax function not inside beforeRender

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top