JSON文件代码如下:
[
{
"name":"炭火烤肉",
"imgsrc":"img/classification.jpg",
"average":167,
"address":"综合楼3楼",
"classify":"bbq",
"recommendation":[
{"food":"和牛拼盘","price":198},
{"food":"精选套餐","price":320},
{"food":"特选牛舌","price":58}
]
}
]
script代码如下:
$.ajax({
url: "json/food.json",
type: "post",
dataType: "json",
success: function(data) {
$.each(data.recommendation, function(i, item) {
var str = '<div>店名:' + item.food + '人均:' + item.price + '</div>';
document.write(str);
})
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus); // parser error;
}
});
目前的问题是:
$.each()函数里的data.recommendation貌似不能这么用(?),alert显示“内部服务器错误”,错误代码是500,4。
尝试着改成data并把后面的item.food ,item.price改成item.name,item.average能正常获取json(json路径没错)。那如果我想获取recommendation里的food和price该怎么改代码呢?
慕标琳琳
相关分类