I have been using $.getJSON in order to verify if a script has done its job properly, using exit("{'status':true}") (or false) but now I need the script to return a value (or an array). I see that I can't use exit('{ "status": $myarray}');. What can I use instead? I am new to php- is it possible to do something like return '{ "status": $myarray}'; or something alike?
Thanks in advance
Add a comment
|
5 Answers
You can use the following function:
json_encode($data);
1 Comment
Mihai Bujanca
And exit($data)? How will the ajax recieve the response?
In your php, use json_encode as such:
<?php
header('Content-Type: application/json');
echo json_encode($myarray);
// or, `exit(json_encode($myarray))` if global scope (see *REF)
?>
and in your jQuery use getJSON normally:
$.getJSON("test.php",function(result){
...
});
1 Comment
Manas
header('Content-Type: application/json') did the trick, thank you! `$jsonString = 'your json string'`
$jsonArray = json_encode($jsonString);
return $jsonArray
1 Comment
Beetroot-Beetroot
I think the question is about encoding not decoding.