我无法获取通过 AJAX 调用返回的响应 JSON 数据。这是我在 Firebug 中得到的结果。
{"COLUMNS":["ID","NAME","REGION","EMAIL"],"DATA":[["1234","John Doe","West","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0dadfd8ded4dfd5f0ddc9d4dfddd1d9de9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>"]]}
这是我的脚本:
$.ajax({
url: "action.cfc?method=getEmployees&returnformat=json&queryFormat=column", type: "POST",
success: function(response){
console.log(response);// is the data above
console.log(response.DATA.NAME[1]); // this doesn’t work
console.log(response.DATA['NAME']);//and this doesn’t work
}
});
当我尝试控制台记录名称时,我在 fireBug“response.DATA is undefined”中收到此错误。我缺少什么?预先感谢您的帮助!
最佳答案
response.DATA
是一个数组数组,因此您需要为每个级别建立索引。
console.log(response.DATA[0][1]);
编辑:如果您想使用键/值对,您需要在 CF 中创建一个 struct
而不是数组
您还应该在 $.ajax 中设置 dataType:'json'
关于jquery - 使用 jQuery 显示 ColdFusion JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642717/