jQuery JSON数据使用来自数组的单个响应

时间:2016-05-24 13:24:34

标签: jquery json

尝试从JSON响应填充节的2个区域。响应工作并输出以下内容,但是当我尝试使用数据[0]和数据[1]来获取它不想要的标题,文本和图像时。我正在正确地获取数据,但似乎无法将其添加到其中.console.log响应未定义。

JSON响应:

[{"title":"Title 1","text":"Text 1","image":"Image 1"},{"title":"Title 2","text":"Text 2","image":"Image 2"}]

jQuery代码:

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
    console.log(data[0].title);
    // Populate both sections with returned data
});

1 个答案:

答案 0 :(得分:1)

像这样使用

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
                console.log(data[0].title);
                    // Populate both sections with returned data


            },"json");

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
                data = JSON.parse(data);
                console.log(data[0].title);
                    // Populate both sections with returned data


            });