有没有类似于'finally'jQuery中的AJAX调用

Is there any analog to a 'finally' in jQuery AJAX calls?

本文关键字:AJAX 调用 中的 jQuery 有没有 finally 类似于      更新时间:2023-09-26

在jQuery AJAX调用中是否存在Java 'finally'类比?这里有这个代码。在我的always中,我抛出一个异常,但是我总是希望它转到then()方法。

    call.xmlHttpReq = $.ajax({
        url : url,
        dataType : 'json',
        type : 'GET'
    }).always(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
       throw "something";
    }).then(function() {
        alert("i want to always run no matter what");
    });

我已经尝试使用done()complete()和另一个always(),但似乎没有任何工作。

这里是JSFiddle:

http://jsfiddle.net/qv3t3L0m/

请看下面的例子:

$.ajax({
        type: "GET",
        dataType: dataType,
        contentType: contentType,
        async: TRUE,
        url: $('html form:nth-child(1)').attr('action') + "?" $('html form:nth-child(1)').serialize(),
        success: function(data) {
            console.log("FUNFOU!");
        },
        error: function(data) {
            console.log("NÃO FUNFOU!");
        },
        complete: function(data) {
            console.log("SEMPRE FUNFA!"); 
            //A function to be called when the request finishes 
            // (after success and error callbacks are executed). 
        }
    });

更多信息:http://api.jquery.com/jquery.ajax/

.always()应该可以工作。参见http://api.jquery.com/jQuery.ajax/的 jqXHR对象一节。

jqXHR。always(function(data|jqXHR, textStatus, jqXHR|errorThrown) {});完整回调选项的另一个构造是.always()方法取代已弃用的。complete()方法。

作为对成功请求的响应,函数的参数是与.done(): data、textStatus和jqXHR对象相同。为失败请求的参数与.fail()的参数相同:thejqXHR对象、textStatus和errorThrown。参考deferred.always()查看实现细节。

参见http://api.jquery.com/deferred.always/

下面的建议在jQuery中不起作用,因为jQuery的承诺实现不处理传递给它的方法中抛出的错误。我把它们留在这里只是为了说明如果jQuery是promises/A+兼容的,可能会发生什么。正如Bergi正确指出的那样,你将不得不在自己的try catch块中手动包装代码。

call.xmlHttpReq = $.ajax({
    url : url,
    dataType : 'json',
    type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
   throw "something";
}).always(function() {
    alert("i want to always run no matter what");
});

虽然我不确定是否jquery的承诺支持总是,另一种选择是使用然后(再次)并传递相同的函数作为successHandler和errorHandler,像这样:

call.xmlHttpReq = $.ajax({
    url : url,
    dataType : 'json',
    type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
   throw "something";
}).then(function() {
    alert("i want to always run no matter what");
},
function() {
    alert("i want to always run no matter what");
});

对于那些使用jQuery 3.0及更高版本的人来说

弃用通知:jqXHR.success()、jqXHR.error()和jqXHR.complete()回调从jQuery 3.0开始删除。您可以使用jqXHR.done()、jqXHR.fail()和jqXHR.always()来代替。

官方文档

有一个bug ajax是依赖于服务器的,需要用"complete"来检查状态是最好的,一种"success", "error"等不是100%的PUT, POST和GET…看一个例子

$.ajax({
    url: '/api/v2/tickets/123456.json',
    ....
    ....
    ....
    complete: function(data) { 
        if (data.statusText == "success") { 
            console.log("Sent successfully");
        } else { 
            console.log("Not Sent");
        }
    }
});

对不起,英语不好!欢欣鼓舞;-)

如果您想为所有ajax请求定义一个代码,您可以这样做

$(document).ajaxComplete(function () {
    console.log('ajax complete on doc');
})