2

I'm using JQuery to call a PHP function that returns a JSON string upon success or throws some exceptions. Currently I'm calling jQuery.parseJSON() on the response and if it fails I assume the response contains an exception string.

$.ajax({
            type: "POST",
            url: "something.php",
            success: function(response){
                 try {
                     var json = jQuery.parseJSON(response);
                 }
                catch (e) {
                    alert(response);
                    return -1;
                 }
                 // ... do stuff with json
            }

Can anyone suggest a more elegant way to catch the exception?

Many thanks, Itamar

4 Answers 4

3

Catch the exception in your PHP script - using a try .... catch block - and when an exception occurs, have the script output a JSON object with an error message:

 try
  {
     // do what you have to do
  }
 catch (Exception $e)
  {
    echo json_encode("error" => "Exception occurred: ".$e->getMessage());
  }

you would then look for the error message in your jQuery script, and possibly output it.

Another option would be to send a 500 internal server error header when PHP encounters an exception:

try
  {
     // do what you have to do
  }
 catch (Exception $e)
  {
     header("HTTP/1.1 500 Internal Server Error");
     echo "Exception occurred: ".$e->getMessage(); // the response body
                                                   // to parse in Ajax
     die();
  }

your Ajax object will then invoke the error callback function, and you would do your error handling in there.

Sign up to request clarification or add additional context in comments.

1 Comment

Very useful! I had the same problem with jQuery Ajax (The library didn't catch my exceptions thrown from PHP). Sending the Header with the 500 Http Code has solved my problem. Thanks
2

Well, you can have a global exception handler in PHP that would call json_encode on it then echo it out.

<?php
    function handleException( $e ) {
       echo json_encode( $e );
    }
    set_exception_handler( 'handleException' );
?>

You could then check if, say, json.Exception != undefined.

$.ajax({
            type: "POST",
            url: "something.php",
            success: function(response){
                 var json = jQuery.parseJSON( response );
                 if( json.Exception != undefined ) {
                    //handle exception...
                 }
                 // ... do stuff with json
            }

Comments

0

Catch the exception on the PHP end, and output an error message in JSON format:

echo json_encode(array(
    'error' => $e->getMessage(),
));

Comments

-1
echo json_encode(array(
    'error' => $e->getMessage(),
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.