Rewrite the ajax method to redirect to the logon page when the asynchronous request session expires
Generally, we will judge the logon status in the filter. If we do not log on, we will jump to the logon page. The core java code of the filter is as follows:
UserItem loginUser = (UserItem)request.getSession().getAttribute("loginUser");if(loginUser == null) {response.sendRedirect("/login.action");}
This is no problem for common pages, but it is often not expected when ajax is used. Because the page is not redirected at all, only the source code of the logon page is captured.
On the Internet, a bunch of mediocre people give a bunch of methods, mainly including.
1. The returned string is appended with the logon status)
2. Modify the http header information)
3. Modify jquery source file judgment. (This is a programmer, but you do not need to use the source file. You have to decompress and compress the file due to performance problems. Others may not know that you have changed the source code)
My solution is similar to the idea of the third method. Rewrite the ajax method to implement pre-processing. The Code is as follows:
JQuery (function ($) {// back up jquery's ajax method var _ ajax =$. ajax; // rewrite the ajax method. First, judge that the success function is being executed during login $. ajax = function (opt) {var _ success = opt & opt. success | 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 find the evidence that data is the login page (TAG) if (data. indexOf ('weianjie ')! =-1) {window. location. href = Globals. ctx + "/login. action "; return ;}_ success (data, textStatus) ;}}); _ ajax (_ opt );};});
In the source code of the login page, you can randomly find a place to mark and hide it. For example, my mark is weinianjie. For $. get and $. post, you don't need to mention it. Both methods call the $. ajax method, so the pre-judgment capability is automatically obtained.
As a result, note that the above extensions must be executed before all your ajax requests.