检测Ajax请求由用户操作中断 [英] Detect Ajax request interrupted by user action

查看:214
本文介绍了检测Ajax请求由用户操作中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检测情况下,Ajax调用过程中,然后将其因中断某些用户操作(例如,用户导航到另一个页面)?

I want to detect cases where an Ajax call is in progress and then it is interrupted due to some user action (e.g. user navigating to another page)?

我可以看到,发生这种情况时我onreadystatechange的处理程序被调用当readyState == 4(完成)和状态== 0。

I can see that when this happens my onreadystatechange handler gets called with readyState == 4 (DONE) and status == 0.

现在的问题是,有没有什么办法可以肯定,这确实对应于描述的场景(由于要求中止用户操作)?我想避免屏蔽其他-potentially更dangerous-错误。

The question is, is there any way to be sure that this really corresponds to the described scenario (request aborted due to user action)? I want to avoid masking other -potentially more dangerous- errors.

推荐答案

有关导航到另一个页面,你可以使用 onbeforeunload的事件的用户。

For the user navigating to another page you could use the onbeforeunload event.

例如:

window.onbeforeunload = function() {
    // Check ajax request state and do something accordingly...
}

这篇关于检测Ajax请求由用户操作中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆