我是AJAX / JQuery / JSON的新手,我使用Struts 2来使用AJAX获取JSON对象,我可以看到返回的对象,但我无法迭代它。返回的对象是一个只有一个对象的列表(可能包含更多对象),每个对象也包含一些其他对象的列表。
这就是js文件的样子:
$.getJSON('GetAllSaleItemsAction', {
customerName : debtorSelection
}, function(jsonResponse){
//alert(jsonResponse);
//populate table
var trHtml = '';
//$("tr:has(td)").remove();
jsonResponse = jsonResponse.saleItemsList;
var responseString = JSON.stringify(jsonResponse);
console.log(responseString);
$.each(jsonResponse, function(i, item) {
var saleEntries = $.parseJSON(item.singleSaleEntries);
var saleEntriesString = JSON.stringify(saleEntries);
console.log(saleEntriesString);
var sseString = '';
$.each(saleEntries, function(n, sse){
sseString += sse.item + ' ' + sse.quantity + ' x ' + sse.price + ' = ' + sse.amount;
});
trHtml+= '<tr><td>' + item.date + '</td><td>' + sseString + '</td><td>' + item.saleAmount + '</td><td>' + item.interest + '</td><td>' + item.totalAmount + '</td></tr>';
});
if(trHtml != ''){
$('#saleRecordTable').append(trHtml);
}
});
这里执行第一个console.log但不执行第二个console.log,因为每个函数中的javascript都没有执行:
[{"customerName":"Tester","date":"2015-02-16T17:36:19","debtorId":1,"interest":0,"saleAmount":14000,"saleId":3,"singleSaleEntries":[{"amount":9000,"item":"DAP_50KG","price":900,"quantity":10,"saleEntry":null,"singleSaleId":4},{"amount":5000,"item":"UREA_50KG","price":500,"quantity":10,"saleEntry":null,"singleSaleId":5}],"totalAmount":14000}]
请指导我。
答案 0 :(得分:0)
问题在于var saleEntries = $.parseJSON(item.singleSaleEntries);
将此语句更改为var saleEntries = item.singleSaleEntries;
一切正常,问题是调试点设置为下一个语句,并且由于此行中的错误,控件永远不会到达那里。