Question

Is there anything in the header of an HTTP request that would allow me to differentiate between an AJAX call and a direct browser request from a given client? Are the user agent strings usually the same regardless?

Was it helpful?

Solution

If you use Prototype, jQuery, Mootools or YUI you should find a X-Requested-With:XMLHttpRequest header which will do the trick for you. It should be possible to insert whatever header you like with other libraries.

At the lowest level, given a XMLHttpRequest or XMLHTTP object, you can set this header with the setRequestHeader method as follows:

xmlHttpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

OTHER TIPS

After some research, it looks like the best approach would be to simply specify a custom user agent string when making AJAX calls and then checking for this custom user agent string on the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top