Rewrite the Ajax method to redirect to the logon page when the asynchronous request session expires

Source: Internet
Author: User

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;(So much, are you willing to change it, and you have to change both the front and back ends, so you have to always think about the status when coding)

2. Modify the HTTP header information;(Pack B deep type)

3. Modify jquery source file judgment;(It's barely a programmer, but you try not to use the source code. If you have performance problems, you have to decompress it and compress it. 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.



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.