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_version >= 5)
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} 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("GET", url, true);

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

// Send the request
xmlHttp.send(null);

</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_REQUESTED_WITH']) &&
$_SERVER ['HTTP_X_REQUESTED_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?originatedFromAjax=Y";

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


Regards,
Erwin Moller

.



Relevant Pages

  • My ajax class - not handling multiple requests
    ... I am having my first real attempt at an ajax class as so far Ive ... Im guessing that when one request is in progress the other one is ... var _method; ... // this cond checks if we have already set xmlhttp, ...
    (comp.lang.javascript)
  • Re: AJAX - Query
    ... XMLHttpRequest honors the same caching commands as the browser. ... if your graph is an image, then javascript can just set the img.src to an asp.net page that generates the image (no ajax required). ... xmlHttp = new XMLHttpRequest; ... Now, conventionally, there would be a postback once i click the button "Plot Graph" which populates the UltraChart and displays the Graph. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Problem detecting an Ajax request
    ... var xmlHttp; ... Ajax request. ... I have the folowing function isAjax() which is returning false: ... echo "This is an Ajax request"; ...
    (comp.lang.php)
  • Re: Updated Javascript Best Practices document
    ... synchronization problems using Ajax. ... timing issues by creating a request "list", ... accessible to the sendthe call-backfunction of each request. ...
    (comp.lang.javascript)
  • Re: ajax wilgets javascript
    ... dragable obj start at 0,0 ... library send ajay request 100,100 ... ajax response form value from server and replace user ... link open a new window ...
    (comp.lang.javascript)