Page 1 of 1

Detecting if a request is Ajax

Posted: Thu May 10, 2007 6:56 am
by Ollie Saunders
I'm writing the controller part of MVC for a home-grown framework at the moment. I've decided I want to have separate controller actions for handling Ajax request than to normal ones.

I have a request builder object that constructs the request object, this object has to set a property "isAjax" on the request. What can I check for to set such a property?

Posted: Thu May 10, 2007 7:11 am
by CoderGoblin
I think the Jscript and Prototype libraries both set a header but I am not 100% certain. You may want to look at what they do.

Posted: Thu May 10, 2007 10:31 am
by nickvd
CoderGoblin wrote:I think the Jscript and Prototype libraries both set a header but I am not 100% certain. You may want to look at what they do.

Code: Select all

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  define('IS_AJAX',true);
}

Posted: Thu May 10, 2007 10:33 am
by Ollie Saunders
Cool. Thanks guys.

Posted: Thu May 10, 2007 10:13 pm
by Kieran Huggins
@nickvd: nice 8)

Posted: Thu May 10, 2007 10:19 pm
by nickvd
8)