Is there a way around the jQuery error handler?

If my Ajax call returns a successful result, but when I process the result, I throw an exception, an error handler fires. This seems intuitive to me, because I believe that an error handler should only work when an error occurs as a result of creating an Ajax request or using a server error. I am trying to use the Ajax function in unit test, so I would like to tell the difference between two different failure scenarios.

+4
source share
2 answers

Forgive me if I interpret this completely wrong, but it looks like you are looking specifically for .ajaxError() handler , which you can use the following:

 $(document).ajaxError(function(event, xmlHttp, options, error) { alert(error); }); 

Or you can bind it, this is an ajaxError event, like a click event. This is only for AJAX errors instead of any jQuery throws, is that what you are after?

+1
source

I just checked the test with the below. It does not throw an error if HTTP 200 is passed back. The var 'result contains everything you expect. Remove the data type if you do not 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 use this as a general success / error method for interacting with a web service.

 / * 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
   };
 }
0
source

Source: https://habr.com/ru/post/1311154/


All Articles