Home > Backend Development > PHP Tutorial > Example of method to determine ajax request

Example of method to determine ajax request

小云云
Release: 2023-03-20 18:30:01
Original
1914 people have browsed it

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:

Example of method to determine ajax request

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
}
Copy after login

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

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!

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