gpt4 book ai didi

javascript - 如何在DataTables 中绑定(bind)Openweather map API 数据?

转载 作者:行者123 更新时间:2023-11-30 20:51:11 24 4
gpt4 key购买 nike

我正在使用 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 warning: table id=weatherTable - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

请帮忙?

最佳答案

使用 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用于代码和演示。

关于javascript - 如何在DataTables 中绑定(bind)Openweather map API 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48163569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com