tags:

views:

788

answers:

2

How does Kohana determine if a request is an AJAX one?

Is there anything different in the referrer string? Do I need to add a GET param, perhaps ?ajax=true

Thank you.

+5  A: 

It checks if the request is made by XMLHttpRequest since most browser send a header in this case with this indication: header HTTP_X_REQUESTED_WITH would be set to XMLHttpRequest.

txwikinger
Ah you learn something new every day! Thanks!
alex
It would appear from Jonathan Sampson's answer that it is not the browser, but the framework that is setting the header.
alex
Well.. the header is send by the browser when the request is made. The framework would not know if the request was made via XMLHttpRequest or a normal http GET if the client (browser) would not indicate it.
txwikinger
+2  A: 

As of v2.3.4

/**
 * Tests if the current request is an AJAX request by checking the 
 * X-Requested-With HTTP request header that most popular JS frameworks 
 * now set for AJAX calls.
 *
 * @return  boolean
 */

public static function is_ajax()
{
  return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND 
          strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
}
Jonathan Sampson
Thanks! You're a useful necromancer!
alex