Rewrite ajax to implement session timeout and jump to the login page (graphic tutorial)

亚连
Release: 2018-05-22 15:38:24
Original
1163 people have browsed it

This article mainly introduces the example code of rewriting ajax to implement session timeout to jump to the login page. Friends who need it can refer to the following

Question: When using window.location.href to jump to the page, The backend only needs to implement a filter to redirect to the login page if the session times out. But what about using ajax? Using ajax to execute will result in a 302 error and it is impossible to jump to the page. Below I will post my front-end and back-end code to address this issue.

1. Session filter

import java.io.IOException; 

0 || requestUri.indexOf("/system/login") > 0) { return ; }

Copy after login

2. Add configuration to web.xml:

 sessionFilter com.manager.filter.SessionFilter   sessionFilter /manager/* 
Copy after login

*3 , Rewrite ajax

Note: This code 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); //如果请求发生错误,会返回登陆页面源代码,如果源代码里面存在lovnx这个字符串,前端就重定向到登陆页面 var reData = XMLHttpRequest.responseText + ""; if(reData.indexOf('lovnx') != -1) { window.location.href="/manager/login.html" rel="external nofollow" ; return; } } }); return _ajax(_opt); }; });
Copy after login

4. Add code to the login page

Copy after login

The above is I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Native JS implements Ajax cross-domain request flask response content (graphic tutorial)

Ajax implements website hijacking Detection method

Ajax front-end and back-end cross-domain request processing method (graphic tutorial)

The above is the detailed content of Rewrite ajax to implement session timeout and jump to the login page (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!