Returning JSON from PHP to JavaScript?
Php has an inbuilt JSON Serialising function.
json_encode
json_encode
Please use that if you can and don't suffer Not Invented Here syndrome.
Here are a couple of things missing in the previous answers:
-
Set header in your PHP:
header('Content-type: application/json'); echo json_encode($array);
json_encode()
can return a JavaScript array instead of JavaScript object, see:
Returning JSON from a PHP Script
This could be important to know in some cases as arrays and objects are not the same.