views:

54

answers:

5

Greetings,
I was trying to discover a proper way to send captured errors or business logic exceptions to the client in an Ajax-PHP system. In my case, the browser needs to react differently depending on whether a request was successful or not. However in all the examples I've found, only a simple string is reported back to the browser in both cases. Eg:

if (something worked)
    echo "Success!";
else
    echo "ERROR: that failed";

So when the browser gets back the Ajax response, the only way to know if an error occurred would be to parse the string (looking for 'error' perhaps). This seems clunky.

Is there a better/proper way to send back the Ajax response & notify the browser of an error?
Thank you.

+3  A: 

You could send back an HTTP status code of 500 (Internal server error) , and then include the error message in the body. Your client AJAX library should then handle it as an error (and call an appropriate callback etc.) without you having to look for strings in the response.

Tom Haigh
+1 jQuery for example accepts different callback functions for successful and failed requests in its Ajax methods.
Techpriester
+1  A: 

I generally send back a JSON response like this:

$response = array('status' => 'error', 'message' => 'an unknown error occured');

if( some_process() ) {
    $response['status'] = 'success';
    $response['message'] = 'Everything went better than expected.';
} else {
    $response['message'] = "Couldn't reticulate splines.";
}

die( json_encode($response) );

So, I can check the status of response.status in my JavaScript and look for a value of "sucess" or "error" and display response.message appropriately.

Adam Backstrom
Chris Z
+1  A: 

Hello, showing to the user a status of what is happening and what has happened is very important.

I you want to structure your ajax responses, you should look into the json format.

if (something worked)
    echo '{ "error": 0 }';
else
    echo '{ "error": 1 }';

Once you set foot into the json world, you will be able to send more structured output. For example :

if (something worked)
    echo '{ "error": 0 }';
else
    echo '{ "error": 1, "code": 889, "desc": "Something bad happened" }';

When you receive this output in javascript, you can transform it into an object and take actions depending on the different keys.

The library json2.js will help you transform your output into an object

Jerome WAGNER
A: 

Sending the appropriate http headers should do the trick and tell your ajax scripts to execute the right callback. Each javascript framework i know of has a success and error callback for it's XHR requests.

header('HTTP/1.1 500 Internal Server Error');

ChrisR
+1  A: 

You can send back a JSON object which contains a custom error code and error message which you can then either handle or display directly to your users:

{
     "response": 10,
     "message": "The database didn't work or something"
}

It would work for success as well:

{
     "response": 1,
     "message": "It worked! Yippee!"
}
John Conde