我应该为新的 jQuery AJAX 代码使用 .done() 和 .fail() 而不是成功和错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10931836/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:58:28  来源:igfitidea点击:

Should I use .done() and .fail() for new jQuery AJAX code instead of success and error

jquery

提问by Alan2

I have coded like this:

我已经这样编码了:

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        alert(ajaxContext.responseText)
    }
});

But when I look at the jQuery .ajax()documentationat the end it seems to suggest I should be coding like this below or at least it suggests adding a .done()and a .fail():

但是当我最后查看 jQuery.ajax()文档时,它似乎建议我应该像下面这样编码,或者至少它建议添加 a.done()和 a .fail()

var request = $.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID }
});

request.done(function (data) {
    xxx;
});
request.fail(function (jqXHR, textStatus) {
    xxx;
});

Update

更新

If I code like this is it the same or is there some advantage to breaking it into three ?

如果我这样编码是一样的还是将它分成三个有什么好处?

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID }
}).done(function (data) {
    xxx;
}).fail(function (jqXHR, textStatus) {
    xxx;
});

采纳答案by Michael Laffargue

As stated by user2246674, using successand erroras parameter of the ajax function is valid.

如user2246674所述,使用successerror作为ajax函数的参数是有效的。

To be consistent with precedent answer, reading the doc :

为了与先前的答案一致,请阅读文档:

Deprecation Notice:

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知

jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调将在 jQuery 1.8 中被弃用。要为最终删除代码做好准备,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

If you are using the callback-manipulation function (using method-chaining for example), use .done(), .fail()and .always()instead of success(), error()and complete().

如果您正在使用回调操作函数(例如使用方法链),请使用.done(), .fail()and.always()而不是success(), error()and complete()

回答by Ivijan Stefan Stipi?

I want to add something on @Michael Laffargue's post:

我想在@Michael Laffargue 的帖子中添加一些内容:

jqXHR.done()is faster!

jqXHR.done()是比较快的!

jqXHR.success()have some load time in callback and sometimes can overkill script. I find that on hard way before.

jqXHR.success()在回调中有一些加载时间,有时可能会过度使用脚本。我以前发现这很难。

UPDATE:

更新:

Using jqXHR.done(), jqXHR.fail()and jqXHR.always()you can better manipulate with ajax request. Generaly you can define ajax in some variable or object and use that variable or object in any part of your code and get data faster. Good example:

使用jqXHR.done()jqXHR.fail()并且jqXHR.always()可以更好地与Ajax请求处理。通常,您可以在某个变量或对象中定义 ajax,并在代码的任何部分使用该变量或对象并更快地获取数据。好的例子:

/* Initialize some your AJAX function */
function call_ajax(attr){
    var settings=$.extend({
        call            : 'users',
        option          : 'list'
    }, attr );

    return $.ajax({
        type: "POST",
        url: "//exapmple.com//ajax.php",
        data: settings,
        cache : false
    });
}

/* .... Somewhere in your code ..... */

call_ajax({
    /* ... */
    id : 10,
    option : 'edit_user'
    change : {
          name : 'John Doe'
    }
    /* ... */
}).done(function(data){

    /* DO SOMETHING AWESOME */

});

回答by Yasir

In simple words

简单来说

$.ajax("info.txt").done(function(data) {
  alert(data);
}).fail(function(data){
  alert("Try again champ!");
});

if its get the info.text then it will alert and whatever function you add or if any how unable to retrieve info.text from the server then alert or error function.

如果它获取 info.text 那么它会发出警报以及您添加的任何功能,或者如果有任何无法从服务器检索 info.text 然后发出警报或错误功能。

回答by Sheo Dayal Singh

When we are going to migrate JQuery from 1.x to 2x or 3.x in our old existing application , then we will use .done,.fail instead of success,error as JQuery up gradation is going to be deprecated these methods.For example when we make a call to server web methods then server returns promise objects to the calling methods(Ajax methods) and this promise objects contains .done,.fail..etc methods.Hence we will the same for success and failure response. Below is the example(it is for POST request same way we can construct for request type like GET...)

当我们要在旧的现有应用程序中将 JQuery 从 1.x 迁移到 2x 或 3.x 时,我们将使用 .done,.fail 而不是 success,error 因为 JQuery up gradation 将被弃用这些方法。对于例如,当我们调用服务器 Web 方法时,服务器将承诺对象返回给调用方法(Ajax 方法),并且这个承诺对象包含 .done、.fail.. 等方法。因此,我们将对成功和失败的响应相同。下面是示例(对于 POST 请求,我们可以以同样的方式为诸如 GET 之类的请求类型构造...)

 $.ajax({
            type: "POST",
            url: url,
            data: '{"name" :"sheo"}',
            contentType: "application/json; charset=utf-8",
            async: false,
            cache: false
            }).done(function (Response) {
                  //do something when get response            })
           .fail(function (Response) {
                    //do something when any error occurs.
                });