我知道之前发布了很多 JSON 解析问题,但没有一个对我有帮助。
I am sending JSON back to my page from PHP via ajax. According to JSONLint.com I am using valid JSON.
My issue is everytime I go to access properties of my object they come back as undefined. I console.log
the json object and it comes out fine. I am confused!
PHP
$returnData[] = array("trainingAccess" => "$trainingAccess",
"destinationUrl" => "$destinationUrl", "errorMessage" => "$errorMessage");
echo json_encode($returnData);
JS
$.ajax({
type: "POST",
url: "submit_login.php",
data: {
userName: $("#userName").val(),
password: $("#password").val()
},
success: function(data) {
var obj = JSON.parse(data);
console.log(obj);//Works
alert(obj.destinationURL);//Doesn't work.
}
Solution
I was making an array of arrays in PHP.
$returnData = array("trainingAccess" => "$trainingAccess",
"destinationUrl" => "$destinationUrl", "errorMessage" => "$errorMessage");