Posted By

anagaiyahoocom on 03/09/11


Tagged

jquery


Versions (?)

Handling Ajax HTTP Error Response


 / Published in: jQuery
 

Example of how to display HTTP error code and message on ajax response

  1. $j.ajax({url: req,
  2. type: "POST",
  3. dataType: "json",
  4. data: "image=" + image + "&lbox=" + lboxVal,
  5. success: good_response,
  6. error: error_response});
  7.  
  8. function error_response(xhr,status)
  9. {
  10. //display http error and message from xhr object
  11. alert('HTTP ' + xhr.status + ' Error Encountered: ' + xhr.statusText);
  12. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: UnseenRevolution on July 28, 2011

I have been using jQuery's ajaxSetup() function to set a default error handler for my Ajax requests.

It looks something link this:
$(function() { $.ajaxSetup({
error: function(jqXHR, exception) { if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.'); } else if (jqXHR.status == 404) {
alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) {
alert('Internal Server Error [500].'); } else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'); } else if (exception === 'timeout') {
alert('Time out error.'); } else if (exception === 'abort') {
alert('Ajax request aborted.'); } else {
alert('Uncaught Error.\n' + x.responseText); }
} });
});

Full code can be found at jQuery Ajax Error Handling Function

You need to login to post a comment.