所以我知道这段代码可以正常工作
$.getJSON('data/book.json', function(jd) {
$('#stage').html('<p> id: ' + jd.data.productTypeList[0].id + '</p>');
$('#stage').append('<p>name : ' + jd.data.productTypeList[0].name+ '</p>');
$('#stage').append('<p> longName: ' + jd.data.productTypeList[0].longName+ '</p>');
});
问题是我正在尝试将其更新到此处并收到错误
var jqxhr = $.getJSON("data/book.json")
.success(function(data, status, xhr) {
$.each(data, function(i,item){
//create book for each item and then insert into the collection
tmpItem=new Book({id:item.data.productTypeList[0].id,category:item.data.productTypeList[0].name,name:item.data.productTypeList[0].longName});
self.add(tmpItem);
});
//dispatch customized event
self.trigger("fetchCompleted:Books");
})
.error(function() { alert("error"); })
.complete(function() {
console.log("fetch complete + " + this);
});
}
但我不能让这个工作,并继续得到item.data的错误未定义。
一旦我排序了我计划将0更改为i以便它将获取阵列中的所有结果
感谢
答案 0 :(得分:2)
您的$.each
错了。
试试这个:
$.each(data.productTypeList, function(i, productType){
// Use "productType" here, example:
// new Book( { id: productType.id } )
}
答案 1 :(得分:0)
在您喜欢的浏览器控制台中包含item.data的行上设置断点。
在那里,查看数据,i和项目变量,并确保它们包含您期望的内容。
确定错误并在那时修复它应该很容易。
答案 2 :(得分:0)
试试这个:
if (!('productTypeList' in data)) {
//catch illegal response
}
$.each(data.productTypeList, function(i,item){
//create book for each item and then insert into the collection
tmpItem=new Book({id:item[i].id,category:item[i].name,name:item[i].longName});
self.add(tmpItem);
});