jQuery check file
I make ajax calls to the file /ajax.php
It gives me some data.
How do I check inside ajax.php, was this file directly opened in browser o开发者_JAVA技巧r by a true ajax call?
Quick and dirty answer is like, you can't make it sure 100%. An "AJAX" request still is a normal HTTP request. Probably the best way to somewhat verify that is to set a custom HTTP header for which you have to check on the server side.
For instance, it's a common practice to add a X-Requested-With
header to the HTTP request if it was an ajax request. jQuery for instance puts that header in all its request under the hood.
I think the usual way PHP developers do this is to pass a flag to the server during the ajax request.
The usual flag I've seen has been: ajax: true
$.ajax({
url : "/ajax.php",
type: "POST", // If you want to send the data as a POST rather than GET
data: ({ajax: true})
});
the best way to make sure this was a true ajax request would be to use tools like firebug, fiddler or the network tab in the google chrome developer tools. you can then observer the traffic between your browser and the server, contents of the traffic etc..
精彩评论