Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  200] [ 3]  / answers: 1 / hits: 99585  / 11 Years ago, fri, august 16, 2013, 12:00:00

The questions:




  1. Should we change our coding as suggested below?

  2. Is there a difference between .done() & success:, .fail() & error: and .always() & complete:?



The preamble:



I was putting together a jQuery.ajax call, which I have done successfully in the past too. Something like this:



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


While taking a quick look at some documentation, I came across a reference stating that The success, error and complete callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.



We should therefore start coding something like this instead:



$.ajax( example.php )
.done(function (data) { someSuccessFunction(data); })
.fail(function (jqXHR, textStatus, errorThrown) { someErrorFunction(); })
.always(function() { alert(complete); });

More From » ajax

 Answers
28

Well there is no advantage of doing that in that particular situation.



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




  1. Attach multiple handlers

  2. Do so anywhere and not just when calling $.ajax



If you are at the $.ajax call site only attaching single handlers then those advantages don't really come into play.



So you can return the promise and others may attach their own handlers.



Example is refreshing plugins after ajax request:



$.ajaxPrefilter(function(opt, origOpt, jqxhr) {
jqxhr.always(function() {
$([data-plugin]).plugin();
});
});

[#76322] Thursday, August 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rayvencallij

Total Points: 93
Total Questions: 80
Total Answers: 85

Location: Argentina
Member since Thu, Mar 18, 2021
3 Years ago
;