Is there a way to handle the error from an ajax request within JQuery such that when the user navigates away from the page, the resulting error is ignored?
是否有一种方法可以处理JQuery ajax请求的错误,以便当用户从页面导航时,结果的错误被忽略?
$(document).ready(function() {
$.ajax( {
url:'/server/url/',
type: 'POST',
data: {some..data},
success:function(response) { do stuff with response },
error: function(xhr, textStatus, errorThrown) {
// Don't raise this alert if user has navigated away from the page
alert('error');
}
});
I'm curious if anybody knows of a clean way to handle this? I know you could create some logic in a $(window).bind("beforeunload", function(){})
method but I'd prefer if there was a way to handle the situation without doing this since Chrome appears to no longer support it. Is there for example a specific error that is returned in this case?
我很好奇是否有人知道一个干净的方法来处理这个问题?我知道你可以在$(窗口)中创建一些逻辑。bind(“beforeunload”,function(){})方法,但是我更希望有一种方法可以在不这样做的情况下处理,因为Chrome似乎不再支持它了。例如,在这种情况下会返回一个特定的错误吗?
Thanks.
谢谢。
3 个解决方案
#1
17
It looks like the answer to this is to examine the jqXHR.status. The XMLHttpRequest spec outlines these steps to set the status:
这个问题的答案似乎是检查jqXHR.status。XMLHttpRequest规范概述了设置状态的这些步骤:
The status attribute must return the result of running these steps:
状态属性必须返回运行这些步骤的结果:
If the state is UNSENT or OPENED, return 0 and terminate these steps.
如果未发送或打开状态,返回0并终止这些步骤。
If the error flag is set, return 0 and terminate these steps.
如果设置了错误标志,返回0并终止这些步骤。
Return the HTTP status code.1
返回HTTP状态码
NOTE also: The error flag indicates some type of network error or request abortion. It is initially unset and is used during the DONE state.
注意:错误标志表明某些类型的网络错误或请求流产。它最初是未设置的,在完成状态中使用。
From what I understand therefore, this code check should fix the issue:
根据我的理解,这个代码检查应该解决这个问题:
if (xhr.status == 0)
alert('error');
1https://web.archive.org/web/20120204040047/http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute
1 https:/ /web.archive.org/web/20120204040047/http:/www.w3.org/TR/XMLHttpRequest/ # status属性
#2
12
Checking the status didn't work for me, but
检查状态对我来说不是工作,但是。
// Ignore incomplete requests, likely due to navigating away from the page
if (jqXHR.readyState < 4) {
return true;
}
in the error handler did (4 being DONE).
在错误处理程序中完成了(已完成4项)。
#3
2
I've had to handle this issue too a couple of times.
这个问题我也得处理好几次。
I'd recommend binding to the $(window).unload
event, setting some variable somewhere, such as in your namespace (App.unloading = true
) and then testing it in ajax error callbacks.
我建议您绑定$(窗口)。卸载事件,在某个地方设置某个变量,例如在名称空间中(app. unload = true),然后在ajax错误回调中测试它。
See http://api.jquery.com/unload/
参见http://api.jquery.com/unload/
#1
17
It looks like the answer to this is to examine the jqXHR.status. The XMLHttpRequest spec outlines these steps to set the status:
这个问题的答案似乎是检查jqXHR.status。XMLHttpRequest规范概述了设置状态的这些步骤:
The status attribute must return the result of running these steps:
状态属性必须返回运行这些步骤的结果:
If the state is UNSENT or OPENED, return 0 and terminate these steps.
如果未发送或打开状态,返回0并终止这些步骤。
If the error flag is set, return 0 and terminate these steps.
如果设置了错误标志,返回0并终止这些步骤。
Return the HTTP status code.1
返回HTTP状态码
NOTE also: The error flag indicates some type of network error or request abortion. It is initially unset and is used during the DONE state.
注意:错误标志表明某些类型的网络错误或请求流产。它最初是未设置的,在完成状态中使用。
From what I understand therefore, this code check should fix the issue:
根据我的理解,这个代码检查应该解决这个问题:
if (xhr.status == 0)
alert('error');
1https://web.archive.org/web/20120204040047/http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute
1 https:/ /web.archive.org/web/20120204040047/http:/www.w3.org/TR/XMLHttpRequest/ # status属性
#2
12
Checking the status didn't work for me, but
检查状态对我来说不是工作,但是。
// Ignore incomplete requests, likely due to navigating away from the page
if (jqXHR.readyState < 4) {
return true;
}
in the error handler did (4 being DONE).
在错误处理程序中完成了(已完成4项)。
#3
2
I've had to handle this issue too a couple of times.
这个问题我也得处理好几次。
I'd recommend binding to the $(window).unload
event, setting some variable somewhere, such as in your namespace (App.unloading = true
) and then testing it in ajax error callbacks.
我建议您绑定$(窗口)。卸载事件,在某个地方设置某个变量,例如在名称空间中(app. unload = true),然后在ajax错误回调中测试它。
See http://api.jquery.com/unload/
参见http://api.jquery.com/unload/