Resolves the interceptor's interception of Ajax requests
Interceptor Configuration:
public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object obj) throws Exception {//Get the decision to log whether the session exists a String token = (string) request.getsession (). getattribute
("token");
String PostID = (string) request.getsession (). getattribute ("PostID");
if (token = NULL | | token = = "") {String xrequested =request.getheader ("X-requested-with");
if ("XMLHttpRequest". Equals (xrequested)) {response.getwriter (). Write ("Isajax");
}else{response.sendredirect ("/m-web/user/tologin");
return false;
} if (PostID = null | | postid = = "") {String xrequested =request.getheader ("X-requested-with");
if ("XMLHttpRequest". Equals (xrequested)) {response.getwriter (). Write ("Isajax");
}else{response.sendredirect ("/m-web/user/tologin");
return false;
return true; }
1. Determine the value of String xrequested =request.getheader ("X-requested-with") to determine whether it is an AJAX request.
2, Response.getwriter (). Write ("Isajax"), write a response data to Ajax, so you can make judgments in Ajax
There are two ways of judging:
1 directly in the Ajax inside make judgments (not recommended)
Success:function (data) {
if (data = = "Isajax") {
window.location.href= "m-web/user/tologin" return
;
}
}
2 to change the Ajax source code and then in the compression, is the overall way to modify (recommended)
if (issuccess) {//If no content
if (status = = 204 | | | s.type = = "Head") {
statustext = "nocontent";
If not modified
} else if (status = = 304) {
statustext = "notmodified";
If We have data, let ' s convert it
} else {
statustext = response.state;
Success = Response.data;
Error = Response.error;
issuccess =!error;
Solve the Ajax interception problem
var result = Responses.text;
if (Result.indexof ("Isajax") >=0) {
window.location.href= "m-web/user/tologin";
return;}}
Thank you for reading, I hope to help you, thank you for your support for this site!