Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle errors from AJAX calls?

Let's say I have the following jQuery AJAX call:

$.ajax({    type: "POST",    url: "MyUrl",    data: "val1=test",    success: function(result){         // Do stuff    }  }); 

Now, when this AJAX call is made I want the server-side code to run through a few error handling checks (e.g. is the user still logged in, do they have permission to use this call, is the data valid, etc). If an error is detected, how do I bubble that error message back up to the client side?

My initial thought would be to return a JSON object with two fields: error and errorMessage. These fields would then be checked in the jQuery AJAX call:

$.ajax({    type: "POST",    url: "MyUrl",    data: "val1=test",    success: function(result){        if (result.error == "true")         {            alert("An error occurred: " & result.errorMessage);        }        else         {            // Do stuff        }    }  }); 

This feels a bit clunky to me, but it works. Is there a better alternative?

like image 343
Kevin Pang Avatar asked Jan 02 '09 18:01

Kevin Pang


People also ask

How do you handle errors in AJAX call?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

What is AJAX error function?

The ajaxError() method specifies a function to be run when an AJAX request fails. Note: As of jQuery version 1.8, this method should only be attached to document.

How do I know if AJAX call failed?

ajax() : $. ajax({ type: 'POST', url: 'page. php', data: stuff, success: function( data ) { }, error: function(xhr, status, error) { // check status && error }, dataType: 'text' });


2 Answers

Personally, I have similar code to the following code in my library.

$.ajaxSetup({     error: function(xhr){         alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);     } }); 

jQuery docs Ajax/jQuery.ajaxSetup

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function. This also means that you do not have to define an error: call in every $.ajax call.

header('HTTP/1.0 419 Custom Error'); 

Quote from w3.org header information

Error 4xx, 5xx The 4xx codes are intended for cases in which the client seems to have erred, and the 5xx codes for the cases in which the server is aware that the server has erred. It is impossible to distinguish these cases in general, so the difference is only informational. The body section may contain a document describing the error in human readable form. The document is in MIME format, and may only be in text/plain, text/html or one for the formats specified as acceptable in the request.

like image 100
Asciant Avatar answered Sep 22 '22 01:09

Asciant


$.ajax({    type: "POST",    url: "MyUrl",    data: "val1=test",    success: function(result){         // Do stuff    },    error: function(request,status,errorThrown) {         // There's been an error, do something with it!         // Only use status and errorThrown.         // Chances are request will not have anything in it.    }  }); 
like image 42
Salty Avatar answered Sep 23 '22 01:09

Salty