Grow your CSS skills. Land your dream job.

Last updated on:

Detect AJAX Request

The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests.

if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
{
       # Ex. check the query and serve requested data
}

Comments

  1. Permalink to comment#

    I think isset() is more appropriate than !empty() in this situation. Still a useful snippet though. :)

    • Pretty much the very first thing empty does is to make sure isset is false. Using isset and empty is redundant in this manner. empty() on an unset var will always return false (and there will be no error…).

      From PHP.net on empty() (http://php.net/manual/en/function.empty.php)

      Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

  2. Permalink to comment#

    So that you don’t get the notice about whether the variable is set or the attribute is set you should use both isset() and !empty().


    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'] && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
    {
    # Ex. check the query and serve requested data
    }

  3. Dani
    Permalink to comment#

    great and practical snip, however, I’d like to know if there is a great or important disadvantage in using this approach when working with synchronized(normal request) and asynchronized(ajax request) requests in the same page.

  4. SmarterGuy
    Permalink to comment#

    The statement “The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests.” is 100% false.

    The browser does not send this header, javascript libraries do. Libraries like jquery, prototype and mootools.

  5. Hi guys,

    I want to check availability of internet connection before every AJAX request. If the connection is available – proceed else give a pop up message- net not available.

    I wrote the javascript to check net connection.But to check if a request is a AJAX request or a normal request if put following filter in my javaScript code:

    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'] && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == ‘xmlhttprequest’))

    I got following error

    $_SERVER is not defined

    Can anyone correct me, if I am going wrong.

    • Will
      Permalink to comment#

      @Suresh, that’s PHP code you’re trying to run inside of JavaScript. $_SERVER is only available to PHP so you’re getting ‘undefined’

  6. Permalink to comment#

    By default, ajax using header: Accept:application/json, text/javascript, */*
    and not ajax have header like Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    css header: Accept:text/css,*/*; it depending file your request.

    If you using php framework like CodeIgniter, you can use $this->input->is_ajax_request(). See http://codeigniter.com/user_guide/libraries/input.html

    By the way, $_SERVER['HTTP_X_REQUESTED_WITH'] is best way for detecting ajax request.

  7. You should credit David Walsh for this, ’cause I know you got it from somewhere and didn’t write it yourself, Chris.

    http://davidwalsh.name/detect-ajax

  8. Kokuswolf
    Permalink to comment#

    There is no need for combining isset() with empty() since empty() does not output any error.

    http://php.net/manual/en/function.empty.php

    In my oppinion its not the right way to detect if a request was made by “ajax”. A request is a request and the result differs only in the wanted document type, what Idham mentioned. So if html, xml or json is wanted, output in this type. In PHP you get this information from $_SERVER["HTTP_ACCEPT"], which exists for every request(type). Maybe this helps in the beginning:

    $t = $_SERVER["HTTP_ACCEPT"];
    if(strpos($t, ‘application/json’) !== false){
    // JSON is wanted
    }
    elseif(strpos($t, ‘application/xml’) !== false){
    // XML is wanted
    }
    else{
    // Fallback to HTML
    }

  9. Thank you Chris! Thats what i was looking for :)

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".