Reputation: 32933
Can anyone tell me if there is any difference between these two jquery calls?
$.get("/foo", function (data, textStatus, jqXHR) {
doThingA()
}).fail(function () {
doThingB()
});
vs
$.ajax({
url: "/foo",
success: function (data, textStatus, jqXHR) {
doThingA()
},
error: function () {
doThingB()
}
});
I'm refactoring some code and just wondering if these two are exactly the same. I think if there is a difference it's most likely to be between fail()
and the error
option. Grateful for any advice, thanks.
Upvotes: 2
Views: 1297
Reputation: 745
There is no difference
It is a shorthand Ajax function, which is equivalent to:
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
Upvotes: 0
Reputation: 13283
Yes, they are the same.
http://api.jquery.com/jquery.ajax/#entry-longdesc
jqXHR.done(function( data, textStatus, jqXHR ) {});
An alternative construct to the success callback option, [...]jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
An alternative construct to the error callback option, [...]
The AJAX methods (.ajax()
, .get()
, etc.) return promises, which have these methods on them.
An example:
var promise = $.get("/api/user/" + userid);
promise.done(function () { /* ... */ });
promise.fail(function () { /* ... */ });
promise.always(function () { /* ... */ });
You can also chain the method calls.
Upvotes: 3