What is the best way within my PHP script to differentiate between a normal browser GUI request and an AJAX request?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Not as such.
You can write your JavaScript in such as way to to leave some sort of identifier in the request headers that you could use though. See the XHR
setRequestHeadermethod.A nice use of HTTP would be to modify the
Acceptheader and then do normal content negotiation. Then (for example), instead of caring if it is Ajax or not, you just care if an HTML response is preferred over a JSON response.Another convention is to use the non-standard
X-Requested-Withheader with a value ofXMLHttpRequest. A number of JavaScript libraries will add this by default to any request using XHR.Either of these techniques will only work with XMLHttpRequest or plugin based Ajax though. You can’t set arbitrary HTTP headers for JSON-P or iframe based Ajax.