Home > Web Front-end > JS Tutorial > body text

How to handle user session failure in Ajax operation

php中世界最好的语言
Release: 2018-04-02 09:44:26
Original
1541 people have browsed it

This time I will bring you how to deal with the failure of Ajax operation user session, what are the precautions for Ajax operation user session failure, the following is a practical case, let's take a look.

When using the spingMVC interceptor to handle the problem of user session failure, when the user session fails, a string of javascript strings will be returned to force the user's browser to jump to the login page. However, when Ajax is used to request data, only a string will be responded after the verification fails, and JavaScript will not be executed. This is because the Ajax request is initiated by the XMLHTTPRequest object rather than the browser. After the verification fails, the server returns The information will be received by the XMLHTTPRequest object and saved in the js object.

In order to deal with this situation, Http requests can be judged in the background first, and Ajax requests can be processed separately from ordinary http requests.
Observing the request header information sent by Ajax, we can find that the header information of the Ajax request will contain X-Requested-With:XMLHttpRequest. Through this, we can determine whether it is an Ajax request.

String requestType = request.getHeader("X-Requested-With");
if(requestType != null && "XMLHttpRequest".equalsIgnoreCase(requestType.trim())) {
  //如果是ajax请求
  response.setHeader("sessionStatus", "timeout");
  response.sendError(601, "session timeout.");
  return false;
}
Copy after login

javascript code, you can set the global default options for Ajax requests, once and for all

//设置Ajax请求的全局默认options
jQuery.ajaxSetup({
  type:'post',
  complete:function(xhr, ts){ //XMLHttpRequest, textStatus
    var sessionStatus = xhr.getResponseHeader('sessionstatus');
    if(sessionStatus == 'timeout') {
      alert('页面过期,请重新登录!');
      window.top.location.href = 'Login.htm';
    }
  }
});
Copy after login

The project also uses DataTables to make data tables. I found that the above javascript configuration method does not take effect in datatables. Error messageSee: http://datatables.net/tn/7 You must configure the error attribute of ajax

$('#example').dataTable( {
 "ajax": {
  "url": "findRoles.htm",
  "type": "POST",
  "error": function(xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
    var sessionStatus = xhr.getResponseHeader('sessionstatus');
    if(sessionStatus == 'timeout') {
      alert('页面过期,请重新登录!');
      window.top.location.href = 'Login.htm';
    }
  }
 }
});
Copy after login

I believe you have mastered the method after reading the case in this article, and there will be more exciting things Please pay attention to other related articles on php Chinese website!

Recommended reading:

jQuery+Ajax determines whether the entered verification code passes

How to make a smart search box with Ajax Prompt function

The above is the detailed content of How to handle user session failure in Ajax operation. 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
Popular Tutorials
More>
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!