javascript - How to handle HTTP Response 500 error code through Ajax ".fail()" trigger? -



javascript - How to handle HTTP Response 500 error code through Ajax ".fail()" trigger? -

i running jquery code when saw through firebug console when forcefulness 500 error, code within .fail() method, not beingness fired:

$.ajax({ url: "someaction.do", data: "param1=param&param2=param", datatype: "xml" }).done(function(xml){ //some stuff info }).fail(function(xml){ //some other different stuff info not beingness fired });

in other hand, in java action, set httpresponse status "500" when error occurs, example, posting invalid field trough ajax call, searching in database email not present, , show error trough ajax:

<xml version=bla,bla...> <item> <name>message</name> <value>invalid input data</value> </item> </xml>.

any ideas why? want utilize new methods fail() , done(). i know using statuscode handler.

but allways follow new trends if could. @ to the lowest degree seek that.

thank much!

an alternative approach , substitute direct phone call .done() or .fail() filtering responses through .always() , calling .done() or .fail() through filtering deferred ; maintaining ability process actual net errors same fail() callback

// simulate server-side responses var error = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<item>" + "<name>message</name>" + "<value>invalid input data</value>" + "</item>"; var success = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<item>" + "<name>message</name>" + "<value>success</value>" + "</item>"; // params $.ajaxsetup({ beforesend : function(jqxhr, settings) { var d = decodeuricomponent(settings.data) .match(/data=+.*/)[0] .split(/=|&/); var params = {}; params[d[0]] = {}; params[d[0]][d[1]] = d[2]; params[d[0]][d[3]] = d[4]; jqxhr.data = params; } }); var request = function (url, _xmlresponse, data1, data2) { var _response = new $.deferred(), _request = $.ajax({ url: url, type: "post", data: { // responses xml: _xmlresponse, // params data: "param1="+ data1 +"&param2="+ data2 }, datatype: "xml" }); // check `responsetext` "invalid" , // check `textstatus` actual net error , "error" _request.always(function (xml, textstatus, jqxhr) { console.log(xml, textstatus, jqxhr, "check"); // simulate server-side processing, cont. // if both object passed server , // `param1` , `param2` _do not_ have property // value of @ `params` , `""` empty string - generic "error" , // _and_ returned `responsetext` contain "invalid" , // _or_ `textstatus` : `error` , `reject` `dfd` , phone call `.fail()` // else `resolve `dfd` , phone call `.done()` // check params var test = [xml["data"] || jqxhr["data"]].every(function(v, k) { homecoming !(v.data["param1"] !== "" && v.data["param2"] !== "" ) }) ; // check `xml` responsetext node "invalid" text , // generate `.fail()` var check = $(xml.children) .find("value").is(":contains(invalid)"); homecoming check && test || textstatus === "error" // phone call `.fail()` ? _response.reject([xml, textstatus, jqxhr]) // phone call `.done()` : _response.resolve([xml, textstatus, jqxhr]) }); homecoming _response .done(function (xml) { // `done()` stuff console.log(xml[0], xml[1], xml[2]); var msg = $(xml[0].children).find("value").html(); $("#success") .html(msg + ", " + xml[2].data.data.param1 + xml[2].data.data.param2 ); homecoming this.promise() }) .fail(function (xml) { // `.fail()` stuff console.log(xml[0], xml[1], xml[2], "fail"); var msg = $(xml[0].children).find("value").html() || xml[1] + ", " + xml[2]; $("#error") .html(msg); homecoming this.promise() }); };

could phone call request() , without _xmlresponse param , should handle both generated .fail() (if responsetext contains "invalid") , net error .fail() calls (e.g., statuscode 500; textstatus error) , , .done() calls .

request("someaction.do", "param1_param", "param2_param");

optionally, chain returned promise request @ .always() , data array containing request homecoming values ; after beingness processed @ .done() or fail()

request("someaction.do", "param1_param", "param2_param") .always(function(data) { // `.done()` , `.fail()` stuff , // `data, textstatus, jqxhr` console.log(data[0], data[1], data[2]) })

jsfiddle http://jsfiddle.net/guest271314/ek15krb5/

javascript jquery ajax xmlhttprequest

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -