当用户点击刷新时处理ajax错误 [英] handle ajax error when a user clicks refresh
问题描述
如果这是我应该能够查找的内容,我深表歉意.我想要的所有术语都超载了..
i apologise if this is something i should be able to look up. all of the terms i wanted were way overloaded..
这是我的问题:当我打开一个页面时,它会触发一系列的 ajax 调用.如果我然后按 Shift+Refresh,所有这些 ajax 调用都被视为错误,并在整个页面本身重新加载之前显示它们的错误消息对话框.
here is my problem: when i open a page, it fires off a whole series of ajax calls. if i then press Shift+Refresh, all of those ajax calls are being considered errors, and showing their error message dialogs before the entire page itself reloads.
所以错误是由客户端触发的 - 无论如何我可以找出是否是这种情况所以我可以忽略它吗?例如在 xmlhttprequest 或 ajax 函数中(我使用 jquery btw)
so the error is being triggered by the client - is there anyway i can find out if this is the case so i can ignore it? eg in the xmlhttprequest, or in the ajax function (i am using jquery btw)
推荐答案
[这是对上一个答案的编辑,其中有我已经解决的悬而未决的问题]
[this is an edit from the previous answer, which had outstanding questions that I have since resolved]
当用户通过刷新、单击链接或更改浏览器中的 URL 离开页面时,jQuery 会抛出错误事件.您可以通过为 ajax 调用实现错误处理程序并检查 xmlHttpRequest 对象来检测这些类型的错误:
jQuery throws the error event when the user navigates away from the page either by refreshing, clicking a link, or changing the URL in the browser. You can detect these types of errors by by implementing an error handler for the ajax call, and inspecting the xmlHttpRequest object:
$.ajax({
/* ajax options omitted */
error: function (xmlHttpRequest, textStatus, errorThrown) {
if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0)
return; // it's not really an error
else
// Do normal error handling
});
这篇关于当用户点击刷新时处理ajax错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!