对于我的代码,我以下响应JSON响应
{
"command": "SELECT",
"rowCount": 2,
"oid": null,
"rows": [
{
"name": "Life Insurance Silver",
"product__c": "01t2o000007pd3MAAQ"
},
{
"name": "Life Insurance Gold",
"product__c": "01t2o000007pdqpAAA"
}
],
如何使用Ajax遍历此数组以在表中显示名称和product__c? 此刻,我的电话显示名称和产品均未定义
cols += '<td> ' + row.name + '</td>';
cols += '<td> ' + row.product__c + '</td>';
这是Ajax:
$.ajax({
url: event.target.action,
method: event.target.method,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.log('#### sucessful data retrieve string' + JSON.stringify(data));
$.each(data.rows, function(row) {
console.log('#### ROWS ' + row);
var newRow = $("<tr>");
var cols = "";
var contractName = $("#name").val();
var productName = $("#product__c").val();
cols += '<td> ' + row.name + '</td>';
cols += '<td> ' + row.product__c + '</td>';
newRow.append(cols);
$("#panel tbody").append(newRow);
});
$("#messageContract").text("Success!");
$("#messageC").show();
},
error: function(err) {
errorMessage.text(err.responseJSON.error);
error.show();
}
})
答案 0 :(得分:2)
请更正$ .each循环,使$ .call函数中的第一个参数成为$ .each是索引,第二个是每个迭代项。
$.each(data.rows, function(index,row) {
console.log('#### ROWS ' + row);
var newRow = $("<tr>");
var cols = "";
var contractName = $("#name").val();
var productName = $("#product__c").val();
cols += '<td> ' + row.name + '</td>';
cols += '<td> ' + row.product__c + '</td>';
newRow.append(cols);
$("#panel tbody").append(newRow);
});