三生石@ 2015-04-28 11:24 采纳率: 100%
浏览 14

AJAX呼叫传回「未定义」

AJAX call as follows:

$.get('file.php')
.done(function(data) {
    console.log(data);
});

Console returns:

[{"0":"PS001","code":"PS001"}]

However if amend the log to console.log(data.code) it returns undefined. Also, traversing JSON using each as such:

.done(function, data) {
    $.each(data, function(key, value) {
        console.log(value.code);
    });
)};

results in the following error:

[Error] TypeError: [{"0":"PS001","code":"PS001"}] is not a valid argument for
'in' (evaluating 'b-1 in a')

JSON looks well-formed, so not sure what the issue is?

  • 写回答

4条回答 默认 最新

  • weixin_33738578 2015-04-28 11:28
    关注

    Try this, because your code return json array.

    console.log(data[0].code);  //If you getting JSON format
    

    OR

    console.log(JSON.parse(data)[0].code);  //If you getting JSON as String
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)