noluck noluck - 2 months ago 24
Ajax Question

Kohana 3 Check if ajax request

I'm currently using this to check if the request is an ajax request:

if ( ! Request::$is_ajax || Request::instance() == $this->request)
{
exit;
}


If I try to access the page directly it'll exit, but if I make an ajax request, it'll also exit. I also tried just:

if ( ! Request::$is_ajax)
{
exit;
}


And vice versa, but the script still exits everytime. If I remove the check, the ajax works as normal. Any idea what could be wrong? Thanks.

Answer
  1. When you are using Ajax, Request::instance() === $this->request is TRUE. Use this condition for HMVC calls.
  2. Kohana marks request as Ajax when $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest' condition is TRUE. Check your client headers (with firebug for example), maybe your JS doesnt send this header. Or, may be you are using flash uploader?