2

I saw this code at the jQuery site :

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

   2: // perform other work here ...

     // Set another completion function for the request above
   3:  jqxhr.always(function() { alert("second complete"); });

2 questions :

  1. I heard that the callback functions arguments of success , error , complete being transferred to done , fail , always respectively.

    enter image description here

    So if I want to use the jqXHR obj in always callback , How can i know from where he came (error or success) - because the params order in the method signature is different !!!

    //here it is at place 3

enter image description here

//here it is at place 1

enter image description here

  1. I also heard that deferred objects help us to build the callback functions dynamically. (as we can see in '3:')

    I don't understand why will I ever need it ? by the time I got the Section 3: - the ajax call has already been made and alerted completed. In what scenarios the section 3: comes into action?

4
  • If you're using Firefox and have Firebug you could use console.log(jqXHR) in the always function to see what you get. Commented Nov 7, 2011 at 8:31
  • @Alex are u serious ? I need to check it via runtime ... Commented Nov 7, 2011 at 8:32
  • complete() is not meant to perform operations where it matters if the request was successful or not but to perform generic actions such as cleaning up things, re-enabling a disabled button, etc. Commented Nov 7, 2011 at 8:42
  • ? $.ajax({}).always(function(jqXHR){console.log(jqXHR}); Commented Nov 7, 2011 at 8:45

1 Answer 1

1

complete() is not meant to perform operations where it matters if the request was successful or not but to perform generic actions such as cleaning up things, re-enabling a disabled button, etc.

However, you could check if the first argument === jqxhr (the return value of $.ajax) - if the check is true, you know there was an error.

Sign up to request clarification or add additional context in comments.

1 Comment

you mean to check if its object type ? what about if i did return a json object from server ? how would i distinguish ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.