the problem of Ajax redirection.
The default Ajax is not support redirection, because Ajax itself is a partial refresh, do not reload the page. If you need to use redirection, you can do this in the following ways:
Step one: back-end code transformation.
Determine if redirection is required in the backend (most of the cases are interceptors).
Eg:session expiration judgment, when judging the session has expired, the first to determine whether the request is an AJAX request.
Method:
@Override
public boolean prehandle (HttpServletRequest request,
HttpServletResponse response, Object arg2) throws Exception {
User U = (user) request.getsession (). getattribute ("user");
String type = Request.getheader ("X-requested-with");//XMLHttpRequest
if (U = = null) {
redirect
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";
Response.sendredirect (contextpath+ "/index.jsp");
System.err.println ("Sendredirect");
Forward
if (Stringutils.equals ("XMLHttpRequest", type)) {
Ajax requests
Response.setheader ("Sessionstatus", "TIMEOUT");
Response.setheader ("ContextPath", basepath+ "index.jsp");
Response.setstatus (Httpservletresponse.sc_forbidden);
return false;
} else {
Response.sendredirect (basepath+ "index.jsp");
return false;
}
}
return true;
}
Step two: Transform Ajax properties in the previous paragraph.
The $.ajaxsetup () method is used to unify the Ajax method by adding the actions that are performed after the execution of the request finishes.
$.ajaxsetup ({
Set the execution action after the AJAX request ends
Complete:
function (XMLHttpRequest, textstatus) {
Get the response head through XMLHttpRequest, Sessionstatus
var sessionstatus = Xmlhttprequest.getresponseheader ("Sessionstatus");
if (Sessionstatus = = "TIMEOUT") {
var win = window;
while (win! = Win.top) {
Win = Win.top;
}
win.location.href= Xmlhttprequest.getresponseheader ("ContextPath");
}
}
});
Note: This method needs to be introduced, so it is recommended to put this method in the public JS.