Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

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

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() documentation at the end it seems to suggest I should be coding like this below or at least it suggests adding a .done() and 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;
});
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...