1

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.

8
  • Just to note - the question you've linked to is not relevant to this situation, since you're using AJAX to request the result of a PHP script, so it's definitely possible to return a json_encoded representation of a variable from the PHP back to the javascript. Commented Jun 4, 2013 at 13:27
  • @Pudge601 OK, will remove the link. So you're saying it is possible to do what I wish? Commented Jun 4, 2013 at 13:28
  • Yes, if you catch an exception in the webservice, you can return a response containing a json encoded variable describing the error in exactly the same way that you would have sent the response normally. Just remember you would need to let the jquery ajax handling script know that there was an error by either setting the response status code or, if you're using json based RPC, you could include a status:false in the json object Commented Jun 4, 2013 at 13:32
  • @Pudge601 That doesn't sound too different from what I'm doing. When the webservices are down, I'm sent to the error section and receive the console.log in my console. Do I actually have to put return json_encode($x); inside my catch? pass it as a parameter in my error() callback? Commented Jun 4, 2013 at 13:35
  • What do you mean "When the webservices are down"? If the webservice is down, the PHP code won't be running at all, and therefore won't be able to send anything back to the javascript - json encoded or not Commented Jun 4, 2013 at 13:39

2 Answers 2

1

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

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

2 Comments

That's a great start. Now my console log contains it, but how I can extract it out of that huge string? - This is exactly what's returned - jsbin.com/esazes/2/edit (note the "EvaService") string
I have no idea what kind of data that is to even get that string out of there in a sane way. I'd try clearing up those warnings and showing us what what PHP returns before going through the console.
0

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

4 Comments

If the webservices are down, I receive the console.log so I am in the error: part. I just want to get the variable in there too
and what you have in dataError.responseText ?
it looks like you have normal page in there - are you absolutely sure that you telling us all or that you request good URL?
Echoing it, now my console.log returns it, but in the middle of all that other stuff - see here jsbin.com/esazes/2/edit. How can I get the "EvaService" string from that?

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.