Ajax pre-processing enables asynchronous requests to jump to the logon page when the session expires, ajaxsession
In the first blog, mark zhq [0].
Problem description: On the user page, when the session expires or all sessions are logged out, the backend of the common page will have a filter and the session expires and Redirect to the logon page, however, the backend of the ajax request only returns the source code of the logon page and does not jump to the page.
There are many methods on the Internet. 1. The returned string is appended with the logon status. 2. The http header information is modified. 3. The jquery source file is modified for judgment. The following methods are more convenient and easier to maintain. We should not go into details about the disadvantages.
Solution:
JQuery (function ($) {// back up jquery's ajax method var _ ajax =$. ajax; // rewrite the ajax method, $. ajax = function (opt) {var _ success = opt & opt. success | function (a, B) {}; var _ error = opt & opt. error | function (a, B) {}; var _ opt = $. extend (opt, {success: function (data, textStatus) {// if the background redirects the request to the logon page, the source code of the logon page is stored in data, here we need to determine (the login page is generally the source code, so here we only determine whether there is an html Tag) if (data. indexOf ('html ')! =-1) {window. location. href = "http://www.baidu.com"; return;} _ success (data, textStatus) ;}, error: function (data, textStatus) {if (data. responseText. indexOf ('html ')! =-1) {window. location. href = "http://www.baidu.com"; return;} _ error (data, textStatus) ;}}); return _ ajax (_ opt );};});});
When I encounter a split page ajax and request the getlist.html page, the end of sessionwill jump the request to login.html. Here, ajax actually executes an error, so we only need to provide a pre-processing for the error.