Determine if the request is an Ajax request

1. Through the X-Requested-With field in the http request header, determine whether the request is an ajax request,
public static boolean isAjaxRequest(HttpServletRequest request) {
String header = request.getHeader("X-Requested-With");
return header != null && "XMLHttpRequest".equals(header);
}
2. The comparative analysis of packet capture between traditional request and ajax request is as follows:
the two requests have different request headers, Ajax asynchronous request has one more header than traditional synchronous request Parameters

Traditional synchronous request parameters
    accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    accept-charset gb2312,utf-8;q=0.7,*;q=0.7
    accept -encoding gzip,deflate
    accept-language zh-cn,zh;q=0.5
    cache-control max-age=0
    connection keep-alive
    cookie JSESSIONID=1A3BED3F593EA9747C9FDA16D309AF6B   
    keep-alive 300

Ajax asynchronous request method

    accept */*
    accept-language zh-cn
    x-requested-with XMLHttpRequest
    content-type application/x-www-form-urlencoded,text/javascript
    accept-encoding gzip, deflate
    user-agent Mozilla /4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0;
    content-length 233
    connection Keep-Alive
    cache-control no-cache
    cookie CSS=undefined; JSESSIONID=1B9AC25036290F7FB6823CCE1A24E541

You can see that the Ajax request has more x-requested- with , you can use it, if request.getHeader("x-requested-with"); is null, it is a traditional synchronous request, and if it is XMLHttpRequest, it is an Ajax asynchronous request.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326074301&siteId=291194637