Question

I'm new to jquery promises and deferred objects. I'm trying to get or analyze the response of a POST/PUT/DELETE ajax request. I'm looking to be able to read the HTTP status code (200, 204, ...).

When using $.when and the deferred objects in this way:

$.when($.ajax(request)).then(callback_function(response))

It is possible to analyze the HTTP status code? The response vble itself is empty, since there is no data in the response. If it is not possible, Would the solution be to use done() or fail() functions? What if it is important to recognize the HTTP status code to act over it?

Was it helpful?

Solution

Check jQuery documentation http://api.jquery.com/jQuery.ajax/ and search for "The jqXHR Object" there you have the solution.

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information). These methods take one or more function arguments that are called when the $.ajax() request terminates. This allows you to assign multiple callbacks on a single request, and even to assign callbacks after the request may have completed. (If the request is already complete, the callback is fired immediately.) Available Promise methods of the jqXHR object include:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top