Ajax Request Redirection

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.