Rewrite ajax to implement session Timeout jump to the logon page instance code, ajaxsession

Source: Internet
Author: User

Rewrite ajax to implement session Timeout jump to the logon page instance code, ajaxsession

Problem: When you use window. location. href to jump to a page, the backend only needs to implement a filter to redirect to the login page when the session times out. But what about ajax? If ajax is used for execution, a 302 error occurs, and the page cannot be redirected. Next I will post my front-end code for this issue.

1. session Filter

Import java. io. IOException; <p style = "text-align: center">      0 | requestUri. indexOf ("/system/login")> 0) {return;} <p style = "text-align: center">  

2. Add configuration in web. xml:

<filter>  <filter-name>sessionFilter</filter-name>  <filter-class>com.manager.filter.SessionFilter</filter-class></filter><filter-mapping>  <filter-name>sessionFilter</filter-name>  <url-pattern>/manager/*</url-pattern></filter-mapping>

* 3. Rewrite ajax

Note: This code segment is placed on the index page.

JQuery (function ($) {var _ ajax = $. ajax; $. ajax = function (opt) {var _ success = opt & opt. success | function (a, B) {}; var _ opt = $. extend (opt, {success: function (data, textStatus) {_ success (data, textStatus) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {// alert (XMLHttpRequest. responseText); // if a request error occurs, the source code of the logon page will be returned. If the lovnx string exists in the source code, the frontend will redirect to the logon page var reData = XMLHttpRequest. responseText + ""; If (reData. indexOf ('lovnx ')! =-1) {window. location. href = "/manager/login.html" rel = "external nofollow"; return ;}}); return _ ajax (_ opt );};});

4. Add code on the login page

<input type="hidden" value="lovnx">

The above section describes how to rewrite ajax to implement session timeout and jump to the logon page instance code. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.