The first thing you need to understand is what is the primary object (the native object) in the AJAX request? XMLHttpRequest, knowing the object, you can identify the current request by judging the request header property:
One Ajax request header is as follows:
One common 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 Request:");} else{ System.out.println ("Non-Ajax Request:"); RequestType at this time is null}
Similarly, you can restrict a method to accept Ajax requests based on this property:
@RequestMapping (value = "Testparamsandheaders", params = {"username", "age!=10"}, headers = {"X-REQUESTED-WITH=XMLHTTPR Equest "}) public String testparamsandheaders () { System.out.println (" Testparamsandheaders "); return SUCCESS; }