Solution to the problem of user session failure when using Ajax

韦小宝
Release: 2018-01-01 18:36:57
Original
1119 people have browsed it

This article mainly introduces in detail the solution to the problem of user session failure when using Ajax. It has certain reference and value for learning ajax. Friends who are interested in ajax can refer to it

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

DataTables is also used in the project to make data tables. It is found that the above javascript configuration method does not take effect in datatables. For error information, see: 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

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

Related recommendations:

A brief analysis of the problem of json data transmitted from Ajax background success

The occurrence of Ajax transmitting json format data to the background Cause analysis and solution of 415 error

Ajax form submission and file upload example code

The above is the detailed content of Solution to the problem of user session failure when using Ajax. 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
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!