Several solutions to the problem of ajax session expiration
Source: Internet
Author: User
Improper handling may affect user experience and cause inexplicable problems.
The following solutions are provided based on your own ideas and online reference. Each solution has different advantages and disadvantages. Welcome
Let's make corrections.
Solution 1: Check whether the In the web system, when the session expires and the user has an operation, the system generally returns the login interface.
Ask the user to re-enter the user name and password. When the session expires, the content returned by the AJAX request should be the page on the login interface.
Content (the html code of the login page ). Determine whether the returned content uses Var result = request. responseText;/* content returned by ajax */
If (result. indexOf ('<HTML>')>-1) {/* The returned content contains html tags */}
Or
Var r =/If (r. test (result) {/* The returned content contains html tags */}
The above method can be used to determine whether the session has expired and then handle exceptions based on specific services.
Solution 2: The returned result indicates whether the session expires. This is also known as the true/false mode.
This solution is generally used in combination with json.
If the returned result is:
Var res = {
"Result": true,/* The session has not expired, false (the session has expired )*/
"Data": ""/* Other data */
}
If (res ["result"])
{
/* The session has not expired */
} Else {
/* Session expiration */
}
Solution 3: Use Timestamp
Create a global variable on the page
Var startDate;/* The Last Time ajax accessed the server, Date type */
If (new Date (). getTime ()-startDate. getTime () <30*60*1000)
{
/* Assume that the session expires for 30 minutes */
/* The session has not expired */
} Else {
/* Session expiration */
}
Haha! Do not trust the client
Solution 4: extend the session expiration time
This solution has performance problems
4.1: extend the session expiration time
4.2: client round-robin server. (AJAX round-robin server or client, server maintains persistent connection)
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.