编程语言
首页 > 编程语言> > javascript-在jQuery中的延迟对象中解析和拒绝的含义是什么?

javascript-在jQuery中的延迟对象中解析和拒绝的含义是什么?

作者:互联网

我在这里问了一个问题:refresh page if internet connection available,然后@Fabrizio Calderan使用延迟对象实现提供了一个非常简洁的解决方案,如下所示:

setInterval(function() {
    $.when(
        $.ajax({ 
            url  : "/favicon.ico", /* or other resource */
            type : "HEAD"
        })
    )
    .done(function() {
        location.reload();
    });
}, 120000); /* 120000 ~> 2 minutes */

我的问题是:

因为ajax调用将返回jqXHR;如果ajax调用失败怎么办?那么ajax的返回类型是什么?还是jqXHR还是UNDEFINED还是NULL

并且由于ajax的调用将返回jqXHR,这是一个延迟的对象;我可以得出以下结论:

延迟的对象已解决==> jqXHR

或被拒绝==>未定义

问候

解决方法:

jQuery ajax调用总是返回jqXHR对象,无论成功还是失败.它从不返回null或未定义.

可以通过可选的回调通知功能,也可以通过返回的jqXHR对象(作为promise对象)看到成功或失败,从而获得有关aj​​ax调用的完成,成功或失败的promise通知.

由于jqXHR对象也是promise对象,因此您的代码可以简化为:

setInterval(function() {
    $.ajax({ 
        url  : "/favicon.ico", /* or other resource */
        type : "HEAD"
    }).done(function() {
        location.reload();
    });
}, 120000); /* 120000 ~> 2 minutes */

如果除了.done()之外还希望得到其他情况的通知,则可以使用.fail()、. always()或.then().

如果ajax调用立即失败,则Promise将被视为失败(例如,被拒绝).即使已失败,仍将调用附加到promise对象的后续.fail()或.always()处理程序.

以下是相关的jQuery文档:

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the
Promise interface, giving them all the properties, methods, and
behavior of a Promise (see Deferred object for more information).
These methods take one or more function arguments that are called when
the $.ajax() request terminates. This allows you to assign multiple
callbacks on a single request, and even to assign callbacks after the
request may have completed. (If the request is already complete, the
callback is fired immediately.) Available Promise methods of the jqXHR
object include:

jqXHR.done(function( data, textStatus, jqXHR ) {}); An alternative
construct to the success callback option, the .done() method replaces
the deprecated jqXHR.success() method. Refer to deferred.done() for
implementation details.

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {}); An
alternative construct to the error callback option, the .fail() method
replaces the deprecated .error() method. Refer to deferred.fail() for
implementation details.

jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) {
});
An alternative construct to the complete callback option, the
.always() method replaces the deprecated .complete() method.

In response to a successful request, the function’s arguments are the
same as those of .done(): data, textStatus, and the jqXHR object. For
failed requests the arguments are the same as those of .fail(): the
jqXHR object, textStatus, and errorThrown. Refer to deferred.always()
for implementation details.

jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR,
textStatus, errorThrown ) {});
Incorporates the functionality of the
.done() and .fail() methods, allowing (as of jQuery 1.8) the
underlying Promise to be manipulated. Refer to deferred.then() for
implementation details.

标签:ajax,jquery-deferred,javascript,jquery
来源: https://codeday.me/bug/20191029/1962202.html