在Ajax中返回一个PHP变量?

I'm getting a variable inside my catch (for when a web service call fails) statement and attempting to use json_encode on it:

try {
    WebServices::create($this->nameWS);
}
catch (Exception $e) {                  
   $tr = $e->getTrace();
   $x = $tr[3];
   json_encode($x);                     
}

$x contains a string.

This catch statement sends me to the error section of my $.ajax:

$.ajax({
    type: 'POST',
    url: 'index.php',
    data: 'module=random&action=' + action + params,
    dataType: 'json',
    success: function(dataJson){
        callbackServer(action, otherVars, dataJson);
        callServer.isRun = false;
    },
    error : function(dataError) {
        console.log("I want to get the $x variable here");
    }
});

console.logging the dataError parameter returns a huge long list of rubbish, none of which is relevant to this variable.

I have seen it is possible to send a json_encoded variable to JS, but never inside an error block of an ajax return - is there an easy way to get this variable here? Thanks guys.

You need to echo json_encode($x) and send the correct response headers.

you catched exception and return it as nothing happens :) so it will be available in success() function

to make it available in error you must send error header fro e.g. 500 - then js library will detect there's an error and will put output in error() function