Royi Namir
Royi Namir

Reputation: 148524

Double ajax functions?

I saw in jQuery the code :

var jqxhr = $.ajax( "example.php" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

but each ajax Call has also a callback Functions e.g. :

jQuery.ajax({
      ...
      beforeSend: function( xhr ) 
      {

      },
      success: function (data, textStatus, jqXHR)
      {

      }, 
      error: function(jqXHR, error_textStatus, errorThrown)
      {

      },
      complete: function (jqXHR, complete_textStatus)
      {
      }
 });

So what is the difference?

Upvotes: 3

Views: 274

Answers (1)

Gaute Løken
Gaute Løken

Reputation: 7892

success, error etc are implemented internally using the deferred object constructed when calling $.ajax(..). So if you simply call the .done, .fail etc methods, you will get the same behaviour.

The advantage of using the deferred object is that it has some additional functionality that you might want to use. As an example you could perform several async ajax calls, and when they're all complete continue with some work that needs all calls to be complete. Is very nifty tbh. :)

@Matt Ball: No need to be rude about it. Those are indeed the links you should look up however, Royi. They'll tell you what you need to know.

Upvotes: 1

Related Questions