Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

By using jquery ajax function, I can do something like:

$.ajax({    url: url,   type: 'GET',   async: true,   dataType: 'json',   data: data,   success: function(data) {       //Handle server response here    },   error: function(xhr, status, error){      //Handle failure here   }  }); 

I got two questions to ask based on above code:

  1. When will the jquery.ajax() error callback be called??

  2. What if server response to me a json object with string message "There is an error". Which means the request is still send successfully, but I got server response {message: "There is an error"}.

I think no matter what string value server is responsed, if client got server's response, the jquery.ajax() success callback will be triggered anyway.

I'd like to ask if server specifically returns to me a JSON object with string value like {message: 'There is an error'}, could server do something so that this response could be handled in jquery.ajax() error callback instead of success callback?

like image 599
Leem Avatar asked Jun 07 '11 13:06

Leem


People also ask

What triggers Ajax error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

How do you fail an Ajax call?

The function specified by the ajaxError() function is called when the request fails or generates the errors. We can use the fail() callback function as well on the JavaScript promise object( the jqXHR object return by the $. ajax() function) to run the specific function on the ajax request fail.

Which method is used on the returned object of Ajax () method if the Ajax call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.


1 Answers

The error callback will be executed when the response from the server is not going to be what you were expecting. So for example in this situations it:

  • HTTP 404/500 or any other HTTP error message has been received
  • data of incorrect type was received (i.e. you have expected JSON, you have received something else).

In your situation the data is correct (it's a JSON message). If you want to manually trigger the error callback based on the value of the received data you can do so quite simple. Just change the anonymous callback for error to named function.

function handleError(xhr, status, error){      //Handle failure here   }  $.ajax({    url: url,   type: 'GET',   async: true,   dataType: 'json',   data: data,   success: function(data) {      if (whatever) {          handleError(xhr, status, ''); // manually trigger callback      }      //Handle server response here    },   error: handleError }); 
like image 52
RaYell Avatar answered Sep 21 '22 21:09

RaYell