I am having trouble using the json object echoed from php to javascript. In the php file I define
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo($json);
and then in javascript file I want to access this object.
$("#test_btn").click(function() {
$.get("serverside.php", function(data, status) {
console.log("data " , data["a"]); //undefined
console.log(JSON.parse(data)); // error
});
});
I get undefined for data["a"] and an error for JSON.parse. How should I use the returend data?
json_encode
. Don't create your own JSON, it's prone to errors. – elclanrs May 15 at 22:16console.log(data)
and see what it outputs. You're probably not getting valid JSON – itsananderson May 15 at 22:20