jQuery:处理失败的 AJAX 请求的回退
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1844370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Handle fallback for failed AJAX Request
提问by Patrick Oscity
Can jQuery provide a fallback for failed AJAX calls? This is my try:
jQuery 可以为失败的 AJAX 调用提供回退吗?这是我的尝试:
function update() {
var requestOK = false;
$.getJSON(url, function(){
alert('request successful');
requestOK = true;
});
if (!requestOK) {
alert('request failed');
}
}
Unfortunately, even if the callback function of the $.getJSON() method is called, i get the message 'request failed', before the callback function has the opportunity to set the requestOK variable. I think it's because the code runs in parallel. Is there a way to handle such situations? I thought about chaining or some way of waiting for the AJAX request, including its callback function. But how? Does anyone know how to do that?
不幸的是,即使调用了 $.getJSON() 方法的回调函数,我也会在回调函数有机会设置 requestOK 变量之前收到消息“请求失败”。我认为这是因为代码并行运行。有没有办法处理这种情况?我考虑过链接或某种等待 AJAX 请求的方式,包括它的回调函数。但是如何?有谁知道这是怎么做到的吗?
回答by Doug Neiner
You will need to either use the lower level $.ajaxcall, or the ajaxErrorfunction. Here it is with the $.ajax method:
您将需要使用较低级别的$.ajax调用或ajaxError函数。这是 $.ajax 方法:
function update() {
$.ajax({
type: 'GET',
dataType: 'json',
url: url,
timeout: 5000,
success: function(data, textStatus ){
alert('request successful');
},
fail: function(xhr, textStatus, errorThrown){
alert('request failed');
}
});
}
EDITI added a timeout
to the $.ajax
call and set it to five seconds.
编辑我timeout
在$.ajax
通话中添加了一个并将其设置为五秒。
回答by Lasse Skindstad Ebert
Dougs answer is correct, but you actually can use $.getJSON
and catch errors (not having to use $.ajax
). Just chain the getJSON
call with a call to the fail
function:
Dougs 的回答是正确的,但您实际上可以使用$.getJSON
和捕获错误(不必使用$.ajax
)。只需将getJSON
调用与fail
函数调用链接起来:
$.getJSON('/foo/bar.json')
.done(function() { alert('request successful'); })
.fail(function() { alert('request failed'); });
Live demo: http://jsfiddle.net/NLDYf/5/
现场演示:http: //jsfiddle.net/NLDYf/5/
This behavior is part of the jQuery.Deferred interface.
Basically it allows you to attach events to an asynchronous action afteryou call that action, which means you don't have to pass the event function to the action.
此行为是 jQuery.Deferred 接口的一部分。
基本上它允许您在调用该操作后将事件附加到异步操作,这意味着您不必将事件函数传递给该操作。
Read more about jQuery.Deferred here: http://api.jquery.com/category/deferred-object/
在此处阅读有关 jQuery.Deferred 的更多信息:http://api.jquery.com/category/deferred-object/
回答by Jonathon Faust
Yes, it's built in to jQuery. See the docs at jquery documentation.
是的,它内置于 jQuery 中。请参阅jquery 文档中的文档。
ajaxError may be what you want.
ajaxError 可能就是你想要的。
回答by Sean Vieira
I believe that what you are looking for is error option for the jquery ajax object
我相信您正在寻找的是 jquery ajax 对象的错误选项
getJSON is a wrapper to the $.ajax
object, but it doesn't provide you with access to the error option.
getJSON 是$.ajax
对象的包装器,但它不提供对错误选项的访问。
EDIT: dcneiner has given a good example of the code you would need to use. (Even before I could post my reply)
编辑:dcneiner 给出了您需要使用的代码的一个很好的例子。(甚至在我发布回复之前)
回答by thebaron24
I prefer to this approach because you can return the promise and use .then(successFunction, failFunction); anywhere you need to.
我更喜欢这种方法,因为您可以返回承诺并使用 .then(successFunction, failFunction); 您需要的任何地方。
var promise = $.ajax({
type: 'GET',
dataType: 'json',
url: url,
timeout: 5000
}).then(function( data, textStatus, jqXHR ) {
alert('request successful');
}, function( jqXHR, textStatus, errorThrown ) {
alert('request failed');
});
//also access the success and fail using variable
promise.then(successFunction, failFunction);