0

I have a question about the JQuery .post() function for making ajax calls. The question is in the code below:

$.post( "processOrder", $( "#OrderForm" ).serialize(), function() {
          // What is the diff between putting code here....
        })
            .done(function(data) {

               // ... and putting it in the "done" instead ?.... 

              })
            .fail(function() {

              })
            .always(function() {

        });
aDvo
  • 894
  • 4
  • 15
  • 43

1 Answers1

1

Both will only fire if the request is deemed successful and they both pass the same arguments to the callback (data, textStatus, jqXHR). The only difference is .done() is attached in the "promise" style and uses jQuery's Deferred Object implementation, whereas the other is passed as an argument to $.post.

You might consider the .done() method more flexible because you can return or pass the jQuery object elsewhere allowing other code to add a .done() or other deferred handler. That said, you can still use the success handler as an argument and then add a .done() later. Finally, .done() can accept an array of callbacks, all of which will be executed, whereas the success argument only accepts a single function.

MrCode
  • 63,975
  • 10
  • 90
  • 112