如何遍历Ajax数据响应

赫尔曼·欧雷

对于我的代码,我以下响应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();
                }
            })
亚基夫

请更正$ .each循环,以$ .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);
                     });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章