开发者

Valid JSON Array logs Undefined to the console no matter what I do

开发者 https://www.devze.com 2023-03-10 01:24 出处:网络
I\'m using Jquery to call a php script which then generates an array. I\'m using echo json_encode (array ( \"key\"=>$value, \"key\"=>$value, \"key\"=>$value ));

I'm using Jquery to call a php script which then generates an array. I'm using

echo json_encode (array ( "key"=>$value, "key"=>$value, "key"=>$value ));

As the last line of the PHP document which is generating a valid JSON array. I checked via Firebug. Unfortunately when I try to access one of the values with dot-notation, it's coming up as undefined.

$.ajax({
        type        : "POST",
        cache       : false,
        url     :开发者_StackOverflow社区 "/generateArray.php",
        data        : { data: $(this).attr('id') },
        success: function(data) {
                     console.log(data.key);
        }

This is outputting "undefined" to the console, even though when I check the JSON output the array is valid.

I have no idea how to even begin debugging what's wrong with my code. Please help!


Are you sure data is being json decoded on the Javascript side and isn't just a String?

0

精彩评论

暂无评论...
验证码 换一张
取 消