【问题标题】:insert data into table using jquery by reading data from url which returns json通过从返回 json 的 url 读取数据,使用 jquery 将数据插入表中
【发布时间】:2019-06-24 19:56:44
【问题描述】:

以下是我通过 URL(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 报错: 未捕获的类型错误:无法读取未定义堆栈的属性“长度”:类型错误: 无法读取 Object.success 处未定义的属性“长度” (http://localhost:8888/Examples/js/earthquakeUSGS.js:34:39) 在 j

请帮忙解决。

【问题讨论】:

  • 响应数据中没有属性features...它是一个数组,除非显示的 json 不完整。因此feat 未定义且没有length
  • 我是 jQuery 新手,您能帮我读一下表格吗?
  • 试试var feat = resp
  • 查理非常感谢,堆栈溢出很棒。

标签: jquery getjson


【解决方案1】:

这里:

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

没有features 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2018-06-20
    相关资源
    最近更新 更多