Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Detect an Ajax request

A simple way to detect an ajax request.

In your action class.

$this->isAjaxCall = $this->getRequest()->isXmlHttpRequest();

I put the above line in my preExecute where needed, this way the variable is accessible by action and views.

by Fuad Arafa on 2006-10-27, tagged ajax  http  request 

Comments on this snippet

gravatar icon
#1 Pierre Minnieur on 2006-11-07 at 05:29

I'd rather call $this->getRequest()->isXmlHttpRequest() if I want to know if the currently executed action is an ajax call. A preExecute() method making this variable protected viewable for the whole object is just neccessary if I want to check it on every action (than it safes time - otherwise: not).

But I think this snipped is a good help for any ajax newbie who want's to know how he can find out whether the action is called by ajax or not (I use ajax but I have not yet seen that method!) ;) So, thank you!

gravatar icon
#2 Mickael Kurmann on 2007-05-23 at 11:47

Yes, thank you ! Didn't found it in documentation

You need to create an account or log in to post a comment or rate this snippet.