Bug Tracker

Opened 7 years ago

Closed 7 years ago

#14011 closed bug (notabug)

done/fail/always callbacks not called for HTTP status code 202

Reported by: anonymous Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.0.2
Keywords: Cc:
Blocked by: Blocking:


My server returns status code 202 upon successful DELETE request. My client code makes a jQuery AJAX call to issue DELETE request. I tried all combinations of including done/fail/always and success/error/complete callbacks. I see that done/fail/always callbacks are never called, but success/error/complete are called. I also included a callback for statusCode 202, and I see that was called AFTER the success callback was called.

Since success/error/complete callbacks are deprecated, it seems to me that done/fail/always callbacks should be executed in this scenario instead.

The documentation at http://api.jquery.com/jQuery.ajax/ also does not document when statusCode callback will be invoked.

Change History (4)

comment:1 Changed 7 years ago by jaubourg

A jsfiddle example would help a lot but my guess is you're using done/fail/always like options:

$.ajax( {
    done: myDoneCallback
} );

But done/fail/always aren't options, they're methods of the returned jqXHR object:

$.ajax().done( myDoneCallback );

The success/error/complete that are deprecated are the *methods* too, not the options.

// Deprecated
$.ajax().success( myDoneCallback );

// Not Deprecated
$.ajax( {
    success: myDoneCallback
} );

Regarding statusCode, the documentation states:

An object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

       statusCode: {
         404: function() {
           alert("page not found");

If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback.

So a statusCode callback will always be called regardless of the success/error status of the ajax call. Exact, relative timing, isn't given nor should it (because you shouldn't rely on it) but callbacks are called in the order they are added. For options callbacks, I don't remember which comes first though between complete and statusCode callbacks but success/error will be called prior to anything else.

For exact ordering, use the methods.

// Provided the request is successful
$.ajax().done( callNumber1 ).statusCode( {
    202: callNumber2
} ).always( callNumber3 ).statusCode( {
    202: callNumber4
} ).done( callNumber5 );

comment:2 Changed 7 years ago by jaubourg

#14012 is a duplicate of this ticket.

comment:3 Changed 7 years ago by anonymous

Thank you, this addresses my issue. If you could include some or all of this information in the documentation, that would be helpful.

comment:4 Changed 7 years ago by dmethvin

Resolution: notabug
Status: newclosed

The docs seem clear enough, further clarification can be had by asking targeted questions on our forum or StackOverflow. Making the ajax docs longer to clarify obscure points will make it harder to understand.

Note: See TracTickets for help on using tickets.