If my Ajax call returns a successful result, but while processing the result I cause an exception, the error handler fires. This seems counter intuitive to me as I think the error handler should only fire when an error occurs as a result of making the Ajax call or via a server-side error. I am trying to use the Ajax function in a unit test so I would like to tell the difference between the two different failure scenarios.
+1
A:
Forgive me if I'm interpreting this completely wrong, but seems you're looking specifically for the .ajaxError()
handler, which you can use like this:
$(document).ajaxError(function(event, xmlHttp, options, error) {
alert(error);
});
Or you can bind it, it's the ajaxError
event, just like a click
event. This is only for AJAX errors instead of any jQuery throws, is that what you're after?
Nick Craver
2010-05-28 10:00:06
A:
I just ran a test with the below. It will not throw an error if an HTTP 200 is passed back. The var 'result' contains whatever you expect back. Remove the datatype if you don't want to use JSON.
function ajaxRequest(request, url) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: url, data: data, dataType: "json", cache: false, async: false, success: function(result) { //this only happens on success }, error: function(msg,XMLStatus,Err) { ajaxError(msg,XMLStatus,Err); //Call generic error message } }); }
I tend to use this as a generic success/error method for web-service interaction.
/* Data must be prepared in a standard JSON format, either using $.toJSON() for objects, or stringbuilding for individual parameters */ /* General AJAX request handler */ /* serviceName is the full path of the service ie. "fullpath/services/service.asmx/method/" */ function ajaxRequest(data, serviceName, callbackFunction, async) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: serviceName, data: data, dataType: "json", cache: false, async: async, success: function(response) { callbackFunction.Success(response); }, error: function(msg,XMLStatus,Err) { callbackFunction.Error(msg,XMLStatus,Err); } }); } /* Sample use */ function getStuff(a,b,c){ //Sample method signiture: getStuff(string a, string b, string c) var data = "{" data += "'a'" + ":'" + a + "'" data += ",'b'" + ":'" + b + "'" data += ",'c'" + ":'" + c + "'" data += "}"; someParameterImayWantToUseInTheCallBack = "This was the test click button"; serviceName = "/taccapps/samples/servicesample.asmx/getStuff/"; ajaxRequest(data, serviceName, new sampleCallback(someParameterImayWantToUseInTheCallBack), true); } /* Sample callback */ function sampleCallback(someParameterImayWantToUseInTheCallBack){ //someParameterImayWantToUseInTheCallBack is available to both the success and fail methods this.Success(response){ //"response" represents the JSON response from the server. If it is a list/array it can be dotted into using the index. for(item in response){ alert(response[item].a); alert(response[item].b); alert(response[item].c); } }; this.Error(msg,XMLStatus,err){ //Standard HTTP error codes are found in the above }; }
iivel
2010-05-28 19:51:20