JQuery ajax () with success, error and complete vs .done () ,. fail () and always ()

Questions :

  • Should we change our coding as suggested below?
  • Is there a difference between .done() and success: .fail() and error: and .always() and complete: :?

Preamble :

I collected the jQuery.ajax call, which I have successfully done in the past. Something like that:

  $.ajax( { url: someUrl, type: 'POST', data: someData, datatype: 'json', success: function (data) { someSuccessFunction(data); }, error: function (jqXHR, textStatus, errorThrown) { someErrorFunction(); } }); 

After carefully reviewing some of the documentation, I came across a link stating that successful, erroneous, and complete callbacks are deprecated from jQuery 1.8. To prepare the code for possible removal, use jqXHR.done (), jqXHR.fail (), and jqXHR.always () instead.

Therefore, we should start coding something like this:

 $.ajax( "example.php" ) .done(function (data) { someSuccessFunction(data); }) .fail(function (jqXHR, textStatus, errorThrown) { someErrorFunction(); }) .always(function() { alert("complete"); }); 
+65
javascript jquery ajax
Aug 16 '13 at 1:02
source share
1 answer

It is good that there is no advantage in this particular situation.

The point of .done() .fail() .always() methods is that you can

  • Attach multiple handlers
  • Do it everywhere, not just when calling $.ajax

If you are on the $.ajax , where there are only individual handlers, then these benefits do not really come into play.

So, you can return the promise, while others can attach their handlers.

Example of updating plugins after ajax request:

 $.ajaxPrefilter(function(opt, origOpt, jqxhr) { jqxhr.always(function() { $("[data-plugin]").plugin(); }); }); 
+34
Aug 16 '13 at 1:23
source share



All Articles