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该怎么改代码呢?
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
$.getJSON("json/food.json", function(data) {
$.each(data.recommendation, function(i, item) {
var str = '<div>店名:' + item.food + '人均:' + item.price + '</div>';
document.write(str);
});
});
(其实我觉得把post那行删了应该就没问题了,这么写其实就是省字)
添加回答
举报
0/150
提交
取消