Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 1.3: Detect ajax request in view

Tags:

ajax

cakephp

Before Cake 1.3 I would check for an ajax request with a simple $ajax->isAjax(). Now that the AjaxHelper has been deprecated, I'm looking for the proper way to check for an ajax request in my views. I can't find anything in the JsHelper or HtmlHelper methods. This seems like a really simple thing, I hope I'm not missing something obvious!

By the way, I'm aware of the RequestHandler component ($this->RequestHandler->isAjax() in controller), but again I'm looking for help on how to do the ajax detection in my views.

Thanks.

like image 216
Soulriser Avatar asked Oct 13 '10 21:10

Soulriser


2 Answers

Can you not use the RequestHandler component to set a view variable in the beforeFilter function of app_controller?

Something like this:

$this->set('isAjax', $this->RequestHandler->isAjax());

This way you can simply check $isAjax in your views.

like image 53
Chris Hawes Avatar answered Sep 30 '22 02:09

Chris Hawes


In views check if $this->params['isAjax']

like image 21
OpenProjDevel Avatar answered Sep 30 '22 02:09

OpenProjDevel