jQuery JSON回调说'Undefined'

时间:2010-08-24 23:33:25

标签: php jquery json

我正在创建这个脚本,它从JSON中加载数据,该数据在PHP文件中编码(JSON有效)。

当点击div时,它应该显示来自jQuery的数据,这在这里完成:

function quickView(pid) {
    $.getJSON('project.php', { id: pid, view: 'true' }, function(data) {
      $('#view-' + pid).html('<p>' + data.name + '</p>'
        + '<p>' + data.deadline + '</p>');
    });
}

HTML:

<a href="project.php?id=1">
    <div class="row-null">
        <p>An example project</p>
    </div>
</a>
<div class="row-null quick-view" id="view-1" title="Quick View" onclick="quickView(1);">
    <p><img src="img/pixel.png" height="5" width="9" alt="" /></p>
</div>

然而,当我点击它时,它会打开并显示未定义......

输出时的JSON:

[
    {
        "name": "Another example project",
        "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
        "deadline": "Tuesday 24th August 2010"
    }
]

有什么问题?

1 个答案:

答案 0 :(得分:2)

您的服务器正在返回一个数组,因此您需要数据[0] .name。