The jQueryajax global function handles the ajax jump problem after the session expires.

Source: Internet
Author: User
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!

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.