Home  >  Article  >  Backend Development  >  Example of method to determine ajax request

Example of method to determine ajax request

小云云
小云云Original
2018-03-05 10:58:061841browse

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
}

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!

Statement:
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