78

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"); });
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
  • 1
    i prefer the second method... where the promise returned by ajax is used – Arun P Johny Aug 16 '13 at 01:06
  • 9
    The callbacks aren't deprecated. The related methods on the jqxhr object are. –  Aug 16 '13 at 01:08
  • I agree with Arun, also I think thats personally easier to read – Rooster Aug 16 '13 at 01:15
  • 1
    I'm not looking for comments and statements about preference, but would like to understand the differences, if there are any. – PostureOfLearning Aug 16 '13 at 01:21
  • 15
    To those who don't know what jqxhr objects are, what Crazy Train means by "The callbacks aren't deprecated. The related methods on the jqxhr object are." is that `.done()` replaces `.success()`, `.fail()` replaces `.error()`, and `.always()` replaces `.complete()`. The `.success()`, `.error()`, and `.complete()` **methods** are different from the `success`, `error`, and `complete` **options** for the `.ajax()` method. – ahnbizcad Jul 04 '14 at 11:18

1 Answers1

41

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();
    });
});
ahnbizcad
  • 10,491
  • 9
  • 59
  • 85
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 4
    FYI - According to the [docs](http://api.jquery.com/jQuery.ajax/) as of jQuery 1.5 the `complete`, `error` and `success` settings will accept an array of callback functions, so you can attach multiple handlers. – user9645 Oct 23 '13 at 17:49
  • 1
    for #2: where else would these methods be used besides when calling `$.ajax()`? Do these methods only work on jqxhr receivers? – ahnbizcad Jul 04 '14 at 11:59