我有一个json对象数组,由$ .ajax()检索并打印在表中。但是,我无法打印虽然他们通过XAMPP网页检索(在网络中看到>> xhr)。有人可以帮忙吗?
JSON:
{"people":[
{"firstName":"Peter","lastName":"De'Souza","blogURL":"www.google.com"},
{"firstName":"Romeo","lastName":"Cherolov","blogURL":"www.google.com"},
{"firstName":"Caspian","lastName":"Monovola","blogURL":"www.google.com"}
{"firstName":"Sita","lastName":"Martin","blogURL":"www.google.com"}
{"firstName":"Rina","lastName":"Schenoi","blogURL":"www.google.com"}
]}
jQuery:
$.ajax({
type: 'POST',
url: 'http://localhost/example4/data.json',
data: { patientID: "1" },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(jsonData) {
for (i=0;i<5;i++)
{
console.log(jsonData[i].firstName);
}
},
error: function() {
alert('Some Error');
}
});
它只是抛出错误警报,但在网络&gt;&gt; xhr我可以看到'人物 - json对象的数组'被提取。请帮助!
答案 0 :(得分:2)
尝试console.log(jsonData.people[i].firstName);
答案 1 :(得分:0)
将您的循环修改为
foreach(jsonData.people as val)
{
console.log(val['firstName']);
}