Home > Backend Development > PHP Problem > PHP determines whether it is an ajax request

PHP determines whether it is an ajax request

王林
Release: 2023-02-24 20:32:02
Original
2616 people have browsed it

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{ 
    // 正常请求的处理方式 };
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template