如何在DataTables中绑定Openweather地图API数据?

时间:2018-01-09 07:52:05

标签: javascript jquery datatables

我正在使用DataTables并希望在其中显示我的城市数据,但我没有这样做。

例如如果我调用this URL那么我用JSON(来自openweathers map api)获取城市数据。我想在我的数据表中简单地显示这些数据。 请帮忙。

我的代码:

$.ajax({
type: "POST",
url: "http://api.openweathermap.org/data/2.5/group?id=2643741,2644688,2633352,2654675,2988507,2990969,2911298,2925535,2950159,3120501,3128760,5128581,4140963,4930956,5106834,5391959,5368361,5809844,4099974,4440906&appid=de6d52c2ebb7b1398526329875a49c57&units=metric",
dataType: "json",
success: function (result, status, xhr) {

    $('#weatherTable').DataTable({
        data: JSON.stringify(result),
        columns: [
            { data: 'id' },
            { data: 'name' }
        ],
        "pageLength": 3
    });
},
error: function (xhr, status, error) {
    console.log("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}

});

我只是得到错误:

  

DataTables警告:table id = weatherTable - 请求未知   第0行第0列的参数“id”。有关此内容的详细信息   错误,请参阅http://datatables.net/tn/4

请帮帮忙?

1 个答案:

答案 0 :(得分:1)

使用ajax.dataSrc选项指定包含表数据的属性。

var table = $('#example').DataTable({
    ajax: {

        url: 'https://api.openweathermap.org/data/2.5/group?id=2643741,2644688,2633352,2654675,2988507,2990969,2911298,2925535,2950159,3120501,3128760,5128581,4140963,4930956,5106834,5391959,5368361,5809844,4099974,4440906&appid=de6d52c2ebb7b1398526329875a49c57&units=metric',
        dataSrc: 'list',
        method: 'POST'
    },
    columns: [
        { data: "id" },
        { data: "name" },
        { data: "main.temp" }
    ]                       
});

请参阅this example以获取代码和演示。