如何处理AJAX请求中的会话超时

编程入门 行业动态 更新时间:2024-10-16 02:28:01
本文介绍了如何处理AJAX请求中的会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我相信你们都熟悉使用AJAX的投票系统(Um ...看看那边的< ----)

I'm sure you're all familiar with the voting systems that use AJAX (Um... look right over there <----)

我有类似的东西,当你投票支持或下来它使用AJAX从vote.php请求新的值。问题是我正在使用会话来获取用户ID,所以一个人只能投票一次。如果他们在网页上坐了一个小时,然后投票,会话不再存在,会怎么样?什么是处理这种情况的好办法?我应该将页面重定向到登录屏幕吗?如果是这样,我该如何通过AJAX请求引用的votes.php页面来实现?我是否俯视处理这种情况的好办法?任何建议都将是有帮助的。

I have something similar and when you vote up or down it uses AJAX to request the new value from votes.php. The problem is that I am using a session to get the userid so a person can only vote once. What happens if they sit on the page for an hour and then vote so the session is no longer there? What would be a good way of handling this situation? Should I redirect their page to the login screen? If so, how can I do that from the votes.php page that is being referenced by the AJAX request? Am I overlooking a good way of handling this situation? Any advice would be helpful.

推荐答案

考虑返回一个http状态为401,以及一个详细说明原因的JSON对象。如果你正在使用jQuery,那么你就可以把它转到 error()回调,然后你可以解析你的对象。

Consider returning an http status of 401, and a JSON object detailing the reason. If you're using jQuery, that'll drop you to the error() callback, which you can then parse your object.

$.ajax({ data: {}, dataType: 'html', success: function(data) { // do whatever here }, type: 'POST', url: 'myserver', error: function(XMLHttpRequest, textStatus, errorThrown) { // XMLHttpRequest.responseText has your json string // XMLHttpRequest.status has the 401 status code if (XMLHttpRequest.status === 401) { location.href = 'login.php'; } } });

我对PHP不熟悉,但这应该适用于任何环境。您可能必须禁止任何自动登录表单重定向。在asp mvc中,框架将看到401并将默认登录表单推回,状态为200。

I'm not familiar with PHP anymore, but this should work for just about any environment. You may have to suppress any automatic login form redirection though. In asp mvc the framework will see the 401 and push the default login form back, with a status of 200.

更多推荐

如何处理AJAX请求中的会话超时

本文发布于:2023-11-25 23:30:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!