Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a 204 response in jquery ajax?

Tags:

jquery

As I have declared success and error ajax options, where the response is 204, the ajax method goes to option success which leads to an error.

As per the documentation we can use, StatusCode or Complete methods but the disadvantage here is have to declare all the status code like 2?? series, 3?? series, 4?? series! As these response are dynamic and I am not sure about the http status code.

So, which is better way to handle http status code in jquery ajax?

like image 914
John Smith Avatar asked Apr 25 '13 16:04

John Smith


People also ask

Does Ajax wait for response?

ajax() function with the default settings, any JavaScript code after this AJAX request will be executed without waiting for a response. In most cases, this is the desired behavior, but there are some situations where you will want to prevent further execution until there has been a response to the AJAX call.

What is response type in Ajax?

Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.

What triggers Ajax success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


2 Answers

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface. The third argument in the done function is a jqXHR object. This object has a property for the http status code of the result.

jqXHR.done(function(data, textStatus, jqXHR) {}); An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details. link

    $.ajax({
      url: "http://fiddle.jshell.net/favicon.png",
      beforeSend: function ( xhr ) {
          xhr.overrideMimeType("text/plain; charset=x-user-defined"); 
      }
    }).done(function ( data, textStatus, jqXHR) {
       console.log(jqXHR.status); //handle your 204 or other status codes here
    });

Fiddle http://jsfiddle.net/puleos/fVa7X/

Assuming you want to treat all non 200 status codes as an error you could do something like this:

var p = $.ajax({
          url: "http://fiddle.jshell.net/favicon.png",
          beforeSend: function ( xhr ) {
             xhr.overrideMimeType("text/plain; charset=x-user-defined"); 
          }
        });

p.done(function(data, textStatus, jqXHR) {
  if(jqXHR.status !== 200) {
     handleError(jqXHR.status);
     return;
  }
  // Normal processing here
});

p.fail(function(jqXHR, textStatus) {
   handleError(jqXHR.status); 
});
like image 140
Scott Puleo Avatar answered Sep 23 '22 15:09

Scott Puleo


The solution above is a nice solution but for someone who already defines "success" and "error" on many components, this involves a lot of changes of code.

After a reading on the documentation, it is rather easy to get status code on success too :

jQuery.ajax({
..
'success' : function(data,textStatus,jqXHR) {
    if (jqXHR.status == "204") {

    }
},
error : function(jqXHR, exception) {
        // Manage error 
    },

http://api.jquery.com/jquery.ajax/ => statusCode

Then to check status code, a jqXHR.status will do the trick

like image 37
aorfevre Avatar answered Sep 20 '22 15:09

aorfevre