Reputation: 2029
I have a form, which sends data to the server. I process the form with a PHP script, which can return 3 differents JSON strings:
One
exit("{\"success\":\"false\", \"msg\":\"Las claves no cohinciden\"}");
Two
exit("{\"success\":\"false\", \"msg\":\"".$failure->getMessage()."\"}");
Three
exit("{\"success\":\"true\",\"msg\":\"El usuario: $nombreUsuario ha sido dado de alta correctamente.\"}");
The first and second strings, are errors to show. The third string is the normal case. When the script return some of these values, i catch the response with ExtJs do:
var respuesta = Ext.JSON.decode(response.responseText);
But, when do:
console.log(respuesta);
Firebug console says that "respuesta" are undefinied. Any idea ?
Upvotes: 1
Views: 3098
Reputation: 1570
you can use this url for testing your Json code:
http://www.utilities-online.info/xmltojson/#.UNr9FhhQ-gY
Upvotes: 0
Reputation: 7715
When you are outputting JSON for using in AJAX, it's best to set the headers for JSON and return properly formatted JSON. In my below example, we create a normal PHP array with your response and convert it to JSON with json_encode()
.
Try it. I've had issues before with Javascript not taking a string in as JSON before without a proper content type and formatting.
<?php
$response = array("success" => "false", "msg" => "Las claves no cohinciden");
header("Content-type: application/json");
exit(json_encode($response));
?>
Optionally, if you have jQuery... You can do $.parseJSON('your string');
to output JSON from a string.
Upvotes: 2