JQuery Ajax global functions handle the AJAX jump problem after session expiration _jquery

Source: Internet
Author: User

Do the web often need to consider the issue of session expiration, session expiration let the page jump to the login interface, but there is such a problem, when the page expires, the user request backstage way there are two kinds: traditional and asynchronous request way , the traditional way to solve, Request to the background, intercept to the expiration of the operation, direct jump, but the asynchronous request will not refresh the entire page, so the session expiration of the processing can not be the usual way, the need for additional operations.

Specific ideas: The Interceptor to determine whether the AJAX request-is an AJAX request to return a message-page add a global AJAX processing events, the message to judge, if the report session expires, then location to the login page

As a first step, the interceptor determines whether it is an AJAX request:

if (Request.getheader ("X-requested-with")!=null 
&& request.getheader ("X-requested-with"). Equalsignorecase ("XMLHttpRequest")) { 
//is an AJAX request, then return a message to the foreground
printwriter printwriter = Response.getwriter (); 
Printwriter.print ("{sessionstate:timeout}"); 
Printwriter.flush (); 
Printwriter.close (); 
} else{ 
//Not AJAX request, direct jump page
}

The second step is to set up a global Ajax processing event to handle the session expiration problem, similar to an interceptor or filter:

$.ajaxsetup ({ 
contentType: "Application/x-www-form-urlencoded;charset=utf-8", 
Cache:false, 
complete : function (data,ts) { 
//to judge the data returned,
//session expires, location to a page
}} 
);

This is a jquery based on the asynchronous processing mechanism, the complete code I did not write, the last time in the company to write code, can not come out, the article in some of the code is still online to stick in, the early morning has been half an hour, too sleepy, record the train of thought.

Ps:ajax Operation Global Monitoring, user session failure

jquery (function ($) {
//Back up jquery Ajax method 
var _ajax = $.ajax;
Rewrite the Ajax method to first determine that the login is executing the success function 
$.ajax = function (opt) {
var _success = opt && opt.success | | function (A , b) {};
var _opt = $.extend (opt, {
success:function (data, textstatus) {
try {
if (Data.sessionstatus = False) { c21/>//user failure to operate
//return;
} 
catch (e) {
}
_success (data, textstatus);
}
});
_ajax (_opt);
};

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.