Problem detecting an Ajax request

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    Problem detecting an Ajax request

    I have the following code in Javascript which is creating and sending
    an XMLHttpRequest .

    <code>
    var xmlHttp;

    /*@cc_on @*/
    /*@if (@_jscript_vers ion >= 5)
    try {
    xmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
    } catch (e) {
    try {
    xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
    } catch (e2) {
    xmlHttp = false;
    }
    }
    @end @*/
    alert(xmlHttp);
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest( );
    }

    // Build the URL to connect to
    var url = "getURL.php ";

    // Open a connection to the server
    xmlHttp.open("G ET", url, true);

    // Setup a function for the server to run when it's done
    xmlHttp.onready statechange = updatePage;

    // Send the request
    xmlHttp.send(nu ll);

    </code>

    The problem I am having is server side where I am trying to detect an
    Ajax request.
    I have the folowing function isAjax() which is returning false:

    <code>
    <?php

    if(isAjax()){
    echo "This is an Ajax request";
    }
    else{
    echo "Not Ajax";
    }


    function isAjax() {
    return isset($_SERVER['HTTP_X_REQUEST ED_WITH']) &&
    $_SERVER ['HTTP_X_REQUEST ED_WITH'] == 'XMLHttpRequest ';
    }

    ?>
    </code>

    Can someone tell me why isAjax() is failing here?

  • Erwin Moller

    #2
    Re: Problem detecting an Ajax request

    Simon wrote:
    I have the following code in Javascript which is creating and sending
    an XMLHttpRequest .
    >
    <code>
    var xmlHttp;
    >
    /*@cc_on @*/
    /*@if (@_jscript_vers ion >= 5)
    try {
    xmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
    } catch (e) {
    try {
    xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
    } catch (e2) {
    xmlHttp = false;
    }
    }
    @end @*/
    alert(xmlHttp);
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest( );
    }
    >
    // Build the URL to connect to
    var url = "getURL.php ";
    >
    // Open a connection to the server
    xmlHttp.open("G ET", url, true);
    >
    // Setup a function for the server to run when it's done
    xmlHttp.onready statechange = updatePage;
    >
    // Send the request
    xmlHttp.send(nu ll);
    >
    </code>
    >
    The problem I am having is server side where I am trying to detect an
    Ajax request.
    I have the folowing function isAjax() which is returning false:
    >
    <code>
    <?php
    >
    if(isAjax()){
    echo "This is an Ajax request";
    }
    else{
    echo "Not Ajax";
    }
    >
    >
    function isAjax() {
    return isset($_SERVER['HTTP_X_REQUEST ED_WITH']) &&
    $_SERVER ['HTTP_X_REQUEST ED_WITH'] == 'XMLHttpRequest ';
    }
    >
    ?>
    </code>
    >
    Can someone tell me why isAjax() is failing here?
    No I cannot, but why make life harder than needed?
    Why rely on headers that may be filtered away or are not send by every
    implementation of the Object?

    If you want to tell if the request originated from AJAX, just add that to
    the URL, that is 100% foolproof (I think).

    Like:

    var url = "getURL.php?ori ginatedFromAjax =Y";

    And simply check for it:
    if (isset($_GET["originatedFrom Ajax"])){
    // from AJAX
    } else {
    // Not from AJAX
    }


    Regards,
    Erwin Moller

    Comment

    • Simon

      #3
      Re: Problem detecting an Ajax request

      I found the answer, the Ajax request object has to have a custom HTTP
      header. See the following link for more details:




      On Nov 24, 12:07 am, Erwin Moller
      <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
      Simon wrote:
      I have the following code in Javascript which is creating and sending
      an XMLHttpRequest .
      >
      <code>
      var xmlHttp;
      >
      /*@cc_on @*/
      /*@if (@_jscript_vers ion >= 5)
      try {
      xmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
      } catch (e) {
      try {
      xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
      } catch (e2) {
      xmlHttp = false;
      }
      }
      @end @*/
      alert(xmlHttp);
      if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
      xmlHttp = new XMLHttpRequest( );
      }
      >
      // Build the URL to connect to
      var url = "getURL.php ";
      >
      // Open a connection to the server
      xmlHttp.open("G ET", url, true);
      >
      // Setup a function for the server to run when it's done
      xmlHttp.onready statechange = updatePage;
      >
      // Send the request
      xmlHttp.send(nu ll);
      >
      </code>
      >
      The problem I am having is server side where I am trying todetectan
      Ajaxrequest.
      I have the folowing function isAjax() which is returning false:
      >
      <code>
      <?php
      >
      if(isAjax()){
      echo "This is anAjaxrequest";
      }
      else{
      echo "NotAjax";
      }
      >
      function isAjax() {
      return isset($_SERVER['HTTP_X_REQUEST ED_WITH']) &&
      $_SERVER ['HTTP_X_REQUEST ED_WITH'] == 'XMLHttpRequest ';
      }
      >
      ?>
      </code>
      >
      Can someone tell me why isAjax() is failing here?No I cannot, but why make life harder than needed?
      Why rely on headers that may be filtered away or are not send by every
      implementation of the Object?
      >
      If you want to tell if the request originated fromAJAX, just add that to
      the URL, that is 100% foolproof (I think).
      >
      Like:
      >
      var url = "getURL.php?ori ginatedFromAjax =Y";
      >
      And simply check for it:
      if (isset($_GET["originatedFrom Ajax"])){
      // fromAJAX
      >
      } else {
      // Not fromAJAX
      }Regards,
      Erwin Moller

      Comment

      • Erwin Moller

        #4
        Re: Problem detecting an Ajax request

        Simon wrote:
        I found the answer, the Ajax request object has to have a custom HTTP
        header. See the following link for more details:
        >

        >
        OK, good.
        But why make things so hard?
        And will it work for all implementations of XMLHTTPRequest?
        WHy not simply add a name-value pair to the url?

        Regards,
        Erwin Moller
        >
        On Nov 24, 12:07 am, Erwin Moller
        <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        >Simon wrote:
        I have the following code in Javascript which is creating and sending
        an XMLHttpRequest .
        >>
        <code>
        var xmlHttp;
        >>
        /*@cc_on @*/
        /*@if (@_jscript_vers ion >= 5)
        try {
        xmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
        } catch (e) {
        try {
        xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
        } catch (e2) {
        xmlHttp = false;
        }
        }
        @end @*/
        alert(xmlHttp);
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest( );
        }
        >>
        // Build the URL to connect to
        var url = "getURL.php ";
        >>
        // Open a connection to the server
        xmlHttp.open("G ET", url, true);
        >>
        // Setup a function for the server to run when it's done
        xmlHttp.onready statechange = updatePage;
        >>
        // Send the request
        xmlHttp.send(nu ll);
        >>
        </code>
        >>
        The problem I am having is server side where I am trying todetectan
        >Ajaxrequest.
        I have the folowing function isAjax() which is returning false:
        >>
        <code>
        <?php
        >>
        if(isAjax()){
        echo "This is anAjaxrequest";
        }
        else{
        echo "NotAjax";
        }
        >>
        function isAjax() {
        return isset($_SERVER['HTTP_X_REQUEST ED_WITH']) &&
        $_SERVER ['HTTP_X_REQUEST ED_WITH'] == 'XMLHttpRequest ';
        }
        >>
        ?>
        </code>
        >>
        Can someone tell me why isAjax() is failing here?No I cannot, but why
        make life harder than needed?
        >Why rely on headers that may be filtered away or are not send by every
        >implementati on of the Object?
        >>
        >If you want to tell if the request originated fromAJAX, just add that to
        >the URL, that is 100% foolproof (I think).
        >>
        >Like:
        >>
        >var url = "getURL.php?ori ginatedFromAjax =Y";
        >>
        >And simply check for it:
        >if (isset($_GET["originatedFrom Ajax"])){
        > // fromAJAX
        >>
        >} else {
        > // Not fromAJAX
        >}Regards,
        >Erwin Moller

        Comment

        Working...