Handling PHP exceptions with JQuery

Posted by Itamar Bar-Lev on Stack Overflow See other posts from Stack Overflow or by Itamar Bar-Lev
Published on 2010-05-11T10:44:35Z Indexed on 2010/05/11 10:54 UTC
Read the original article Hit count: 280

Hello,

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

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ajax