Home >Backend Development >PHP Tutorial >Example of method to determine ajax request
First of all, we need to understand what is the main object (native object) in the AJAX request? XMLHttpRequest, knowing the object, you can identify the current request by judging the request header attributes:
An AJAX request header is as follows:
A normal get request is as follows:
The sample code is as follows:
String requestType = request.getHeader("X-Requested-With");if("XMLHttpRequest".equals(requestType)){ System.out.println("AJAX请求..");}else{ System.out.println("非AJAX请求.."); //此时requestType为null }
Similarly, the method can only accept AJAX requests based on this attribute:
@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "X-Requested-With=XMLHttpRequest" }) public String testParamsAndHeaders() { System.out.println("testParamsAndHeaders"); return SUCCESS; }
Related recommendations:
A brief discussion on Ajax requests and browser caching
How to solve the problem when AJAX requests contain arrays
Detailed explanation of several ajax request methods that may be encountered in actual combat
The above is the detailed content of Example of method to determine ajax request. For more information, please follow other related articles on the PHP Chinese website!