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


  1. Frank
    Permalink to comment#

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

    • Cameron Spear
      Permalink to comment#

      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 on empty() (

      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. David
    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

    • Cubicle Generation
      Permalink to comment#

      You’re missing the close parenthesis on the isset, teehee.

      *syntax whore*

  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. Suresh
    Permalink to comment#

    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. Idham
    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

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

  7. Mickey Mouse
    Permalink to comment#

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

  8. Kokuswolf
    Permalink to comment#

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

    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
    // Fallback to HTML

  9. Rahil Wazir
    Permalink to comment#

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

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.