使用jQuery显示ColdFusion JSON响应

时间:2012-03-10 00:18:50

标签: jquery json coldfusion

我无法抓取通过AJAX调用返回的响应JSON数据。这就是我在萤火虫中得到的回报。

{"COLUMNS":["ID","NAME","REGION","EMAIL"],"DATA":[["1234","John Doe","West","johndoe@mydomain.com"]]}

这是我的剧本:

$.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”在尝试控制日志名称时。我错过了什么?提前感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

response.DATA是一个数组数组,因此您需要为每个级别编制索引。

   console.log(response.DATA[0][1]); 

编辑:如果你想使用键/值配对,你需要在CF中创建一个struct而不是数组

您还应该在$ .ajax

中设置dataType:'json'

答案 1 :(得分:1)

也许您需要将其从字符串解析为对象:

success: function(response){
    console.log(response);// is the data above
    response = JSON.parse(response)
    console.log(response);// is the data above
}

答案 2 :(得分:0)

Looping through the response recieved, using $.each() jquery function, will be able to access the data returned from CF.
For the above response:
Example,
$.each(response,function(mainKey,mainValue){
    console.log(mainKey); //displays 'COLUMNS' & 'DATA'
    console.log(mainValue);// displays ["ID","NAME","REGION","EMAIL"] & ["1234","John Doe","West","johndoe@mydomain.com"]
    $.each(value,function(innerKey,innerValue){
         console.log(innerValue); //displays 'id','value'....,'1234',..and so on
    });
});