使用jquery通过读取返回json的url中的数据将数据插入表中

时间:2017-07-30 01:45:40

标签: jquery getjson

以下是我通过网址(http://localhost:3000/data

获取的JSON数据
 [{"emp_id":5,"emp_city":"Hyderabad","emp_name":"Sam","emp_phone":"9999999999","emp_sal":"406000"},{"emp_id":1,"emp_city":"Hyderabad","emp_name":"ram","emp_phone":"9999999999","emp_sal":"50000"},{"emp_id":2,"emp_city":"Hyderabad","emp_name":"robin","emp_phone":"9999999999","emp_sal":"406000"},{"emp_id":4,"emp_city":"Hyderabad","emp_name":"krish","emp_phone":"9999999999","emp_sal":"406000"},{"emp_id":6,"emp_city":"Banglore","emp_name":"Bobby","emp_phone":"9999999999","emp_sal":"516000"},{"emp_id":3,"emp_city":"Chennai","emp_name":"rahman","emp_phone":"9999999999","emp_sal":"45000"}]

下面是我试图从JSON上面将数据推送到表的jquery脚本

 (function() {
    // Create the connector object
    var myConnector = tableau.makeConnector();

    // Define the schema
    myConnector.getSchema = function(schemaCallback) {
        var cols = [{
            id: "emp_id",
            dataType: tableau.dataTypeEnum.int
        }, {
            id: "emp_city",
            dataType: tableau.dataTypeEnum.string
        }, {
            id: "emp_name",
            dataType: tableau.dataTypeEnum.string
        }];

        var tableSchema = {
            id: "emp",
            alias:"test",
            columns: cols
        };

        schemaCallback([tableSchema]);
    };

    // Download the data
    myConnector.getData = function(table, doneCallback) {
        $.getJSON("http://localhost:3000/data", function(resp) {
            var feat = resp.features,
                tableData = [];

            // Iterate over the JSON object
            for (var i = 0, len = feat.length; i < len; i++) {
                tableData.push({
                    "emp_id": feat[i].emp_id,
                    "emp_city": feat[i].emp_city,
                    "emp_name": feat[i].emp_name,

                });
            }

            table.appendRows(tableData);
            doneCallback();
        });
    };

    tableau.registerConnector(myConnector);

    // Create event listeners for when the user submits the form
    $(document).ready(function() {
        $("#submitButton").click(function() {
            tableau.connectionName = "Employee Details"; // This will be the data source name in Tableau
            tableau.submit(); // This sends the connector object to Tableau
        });
    });
  })();

以下是我得到的错误 WDC报告了一个错误:  未捕获的TypeError:无法读取未定义堆栈的属性“length”:TypeError:  无法在Object.success中读取未定义的属性“长度”  (http://localhost:8888/Examples/js/earthquakeUSGS.js:34:39)j

请帮忙解决。

1 个答案:

答案 0 :(得分:0)

下面:

var feat = resp.features,
                tableData = [];

没有features属性。