Home  >  Article  >  Web Front-end  >  Ajax handles user session failure

Ajax handles user session failure

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 11:47:091871browse

This time I will bring you Ajax handling of user session failure. What are the precautions for Ajax handling of 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 invalidation, when the user's session fails, a string of javascript strings will be returned to force the user's browser to jump. Go 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, the Http request can be judged in the background first, and the Ajax request can be processed separately from the ordinary http request.
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.");
  returnfalse;
}

JavaScript code 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
    varsessionStatus = xhr.getResponseHeader('sessionstatus');
    if(sessionStatus =='timeout') {
      alert('页面过期,请重新登录!');
      window.top.location.href ='Login.htm'
    }
  }
});

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

I believe you have mastered it after reading the case in this article Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS implementation of chessboard coverage

Detailed explanation of the use of parasitic combined inheritance in js

The above is the detailed content of Ajax handles user session failure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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