How to handle a 204 response in jquery ajax? -


as have declared success , error ajax options, response 204, ajax method goes option success leads error.

as per documentation can use, statuscode or complete methods disadvantage here have declare status code 2?? series, 3?? series, 4?? series! these response dynamic , not sure http status code.

so, better way handle http status code in jquery ajax?

the jqxhr objects returned $.ajax() of jquery 1.5 implement promise interface. third argument in done function jqxhr object. object has property http status code of result.

jqxhr.done(function(data, textstatus, jqxhr) {}); alternative construct success callback option, .done() method replaces deprecated jqxhr.success() method. refer deferred.done() 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 204 or other status codes here     }); 

fiddle http://jsfiddle.net/puleos/fva7x/

assuming want treat non 200 status codes error 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);  }); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -