This article mainly introduces information about jQuery-based Global ajax functions used to process ajax operations after a session expires. If you need a friend, you can refer to the question of session expiration when you do web, when the session expires, the page jumps to the logon interface. However, when the page expires, there are two ways to request the background: traditional and asynchronous, the traditional method is better to solve the problem. The request goes to the background, intercepts expired operations, and jumps directly. However, the asynchronous request does not refresh the entire page. Therefore, the session expiration cannot be handled as usual, additional operations are required.
Specific train of thought: the interceptor determines whether the request is an ajax request -- if the request is an ajax request, a message is returned -- add a global ajax Processing Event on the page to judge the message. If the report session expires, the location is on the logon page.
Step 1: the interceptor determines whether the request is an ajax request:
If (request. getHeader ("x-requested-")! = Null & request. getHeader ("x-requested-"). equalsIgnoreCase ("XMLHttpRequest") {// ajax request, a message is returned to the foreground PrintWriter printWriter = response. getWriter (); printWriter. print ("{sessionState: timeout}"); printWriter. flush (); printWriter. close ();} else {// jump directly to the page if it is not an ajax request}
Step 2: set global ajax processing events to handle session expiration issues, similar to an interceptor or filter:
$. AjaxSetup ({contentType: "application/x-www-form-urlencoded; charset = UTF-8", cache: false, complete: function (data, TS) {// judge the returned data. // if the session expires, the location will go to a page }}});
This is a jQuery-based Asynchronous processing mechanism. I did not write the complete code. The code I wrote last time in the company could not be included. Some of the Code in the article was still found and pasted on the Internet, it's been half an hour in the early morning. It's too difficult. Just record your ideas.
PS: Global Monitoring of ajax operations, user session failure
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) {try {if (data. sessionstatus = false) {// The user fails to perform the operation // return ;}} catch (e) {}_ success (data, textStatus );}}); _ ajax (_ opt );};});
For more articles about jQuery ajax global function processing ajax jump after the session expires, please follow PHP Chinese network!