Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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');
}

Necro-posting because this came up first my google search.

Dunno if Kohana still works this way, but using HTTP_X_REQUESTED_WITH is no longer best practice.

Ajax requests -- all HTTP requests actually -- should send an "Accept" header.

Any server-side process should examine the "Accept" header to determine what content to send in response. One way to do this in PHP is:

  function is_ajax() {
    return $_SERVER['HTTP_ACCEPT'] == 'application/json';
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top