我相信你们都熟悉使用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请求中的会话超时
发布评论