JSON无法正确呈现

时间:2011-06-13 07:06:22

标签: javascript jquery json

我对jQuery很新:

我正在尝试从jQuery.ajax():

渲染我的JSON
$.each(data, function(key, val){
  $(key + val).insertAfter("#some-div");
});

但我得到的是:

1[object Object]
0[object Object]

Firebug说:

[{"slug": ["This field is required"], "title": ["This field is required."]}, {"slug": ["This field is required"], "title": ["This field is required"]}]

怎么了?

1 个答案:

答案 0 :(得分:2)

你得到[object Object]因为“data”中的内容实际上是JSON而不是HTML。

从Firebug输出看起来,JSON包含列表或错误消息。出于某种原因,错误消息包含在方括号([])中,后者将变为数组。

你应该做的是这样的事情:

$.each(data, function(index, value)
{
  $('<li />').text(value.slug[0] + ' ' + value.title[0]).insertAfter('#some-div');
});

分别查看jQuery.eachJSON,了解有关jQuery的.each函数和JSON格式的信息。

相关问题