对于新的jQuery Ajax代码,我应该使用.done()和.fail()而不是success和error吗?

我是这样编码的:

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

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

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


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

更新

如果我像这样编码,它是一样的,还是把它分成三部分有什么好处?

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

As stated by user2246674, using success and error as parameter of the ajax function is valid.

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.

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

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

jqXHR.done() is faster!

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

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:

/* 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 */


});

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.

We will use .done, .fail instead of success, error when we migrate to jQuery from 1.x to 2.x or 3.x in our old existing application because jQuery is going to deprecate these methods. For example, when we make a call to server web methods and the server then returns promise objects to the calling methods (Ajax methods) these promise objects contain .done, .fail, etc. methods. Hence, we will do the same for the success and failure response. Below is an example (it is for a POST request the same way we can construct for a request type like 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.
});