Home >Backend Development >PHP Problem >PHP determines whether it is an ajax request
php determines whether it is an ajax request
Let’s talk about the front-end using jQuery How to distinguish:
When jQuery issues an ajax request, a piece of information named X-Requested-With will be added to the request header. The content of the information is: XMLHttpRequest. You can use $_SERVER[ "HTTP_X_REQUESTED_WITH"] to get. (Note: The dash is replaced by an underscore, which is not case-sensitive)
From this, we can judge whether it is an ajax request:
if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){ // ajax 请求的处理方式 }else{ // 正常请求的处理方式 };
When using native JavaScript to issue an ajax request, we can also add information to the header to facilitate back-end students to distinguish. The method is as follows:
var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","test.php",true); xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest"); xmlhttp.send();
Recommended Tutorial: PHP Video Tutorial
The above is the detailed content of PHP determines whether it is an ajax request. For more information, please follow other related articles on the PHP Chinese website!