I use:
var jqXHR = $.ajax(settings);
jqXHR.success(function(result){});
jqXHR.error(function(result){});
jqXHR.complete(function(result){});
But version 1.5 has added the deferred object.
Q: In general, when do you use success, error and complete methods vs. the new hotness of deferred then, done and fail?
For
$.ajax()and family.successis merely a synonym for Deferred’s.done, and likewise.erroris a synonym for.fail.So in fact the examples you show are already deferred methods, but with different names.
.completeis mostly a synonym for the new jQuery 1.6.always, and you can get the same effect using$.then(cb, cb), which will causecbto be invoked whether the AJAX call succeeds or not. I believe there are minor differences in which parameters are passed to the “fail” callbacks between the.complete,.alwaysand$.thenvariants.I personally prefer to use the Deferred version of those named functions, because then you don’t need to worry about whether your deferred objects are jqXHRs or not. Only
jqXHRshave.success,.error, and.complete, but every Deferred (including jqXHRs) has.done,.failand.always.EDIT it seems the jQuery devs agree with me – they’ve announced that
.success,.errorand.completewill be deprecated in jQuery 1.8