Click to See Complete Forum and Search --> : How to identify an asynchronous request on the server side.


tim042849
11-17-2009, 05:46 PM
I've been using both compiled and scripting languages for CGI applications for
13 years. I have been using javascript for 7 years and ajax methods for 2 years.
I would like to know if there is anything in an asynchronous request that would identify
itself as such a request to a script on the server side.

Certainly there are "rigged" solutions that would include a hidden
field in a form that or directly intercepting the serialization process, both of which I have
used.

However, I am curious as to whether there might be anything in the request headers
that would identify the type of request. I have not been able to identify any myself.

TIA
Thanks
tim

svidgen
11-19-2009, 09:45 AM
You'll have to do some tests. Create a simple page, containing an AJAX request to itself. In the AJAX request add a query to signal "no output" for the script. Have the script log the request headers from every request. Then examine the log and let us know what you see.

My guess: You'll see no significant difference. At best, you can expect to see the REFERRER header populated in "subrequests." But, this will not be sufficient in itself to determine whether something was requested asynchronously.

See http://en.wikipedia.org/wiki/List_of_HTTP_headers.

svidgen
11-19-2009, 12:44 PM
I just discovered a header that FF uses:
X-Requested-With: XMLHttpRequest

Though, this won't catch requests made using other methods (hidden iframe, dynamic script tag). And I can't say whether IE or Safari use this header ....

tim042849
11-19-2009, 12:57 PM
Hi svidgen:
As to your first post, I have already done that and have seen nothing to distinguish
the type of request.
And for your second - I did come across the same header. I expect in time that all modern
browsers will adopt such a feature - and I look forward to that time:)
Here is the approach that I will take for now:
1)Since I can control the validation and serialization of any request thru my own module,
I can (and do) add a "magic" name and value to the query string and my CGI modules for
the server are coded to check for it.
2)Where a web designer does not choose to use my javascript module (as above), than
I can request that they embed the "magic" name and value in an html form.
And of course one must take care to pick a name that is unlikely to be duplicated...
And thank you for this: http://en.wikipedia.org/wiki/List_of_HTTP_headers.
very helpful
regards
tim

svidgen
11-19-2009, 01:08 PM
Cool beans.

Would it possibly be more sensible to assume the request is asynchronous and include an indicator when it is synchronous?

tim042849
11-19-2009, 02:00 PM
Cool beans.

Would it possibly be more sensible to assume the request is asynchronous and include an indicator when it is synchronous?
Hmm! Jon, that is worth considering. What do you think would be the advantages of this
approach?
thanks
tim

svidgen
11-19-2009, 02:32 PM
Well, if it is intended to be used by others asynchronously, it is best to design it to be consumed asynchronously and assume that it is consumed asynchronously, keeping synchronous use a special case. This just better ensures that folks are getting the right stuff, because they (and you) don't have to worry about developers leaving out your asynch-indicator on accident (or on purpose).

That's really all I had in mind.

tim042849
11-19-2009, 02:54 PM
Understood. Thanks very much.
tim