javascript - 使用 ajax 调用填充数据表
问题描述
我正在尝试使用 ajax 调用填充数据表。但它只用来自的最后一个值填充第一列columns
。
这是我的代码:
$(document).ready(function() {
$.ajax({
url: "http://192.168.2.32:5000/get_all_locations",
'method': 'GET',
'contentType': 'application/json'
}).done(function(data) {
console.log('dataaa', data);
$('#myTable').dataTable({
'aaData': data['data'],
"columns": [{
"data": "id",
"data": "name",
"data": "code"
}]
})
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>Code</th>
</tr>
</thead>
</table>
在上图中,id 字段显示代码的值。我该如何解决这个问题?
数据输出
{…}
StatusCode: true
StatusDescription: "Location details has been pulled successfully."
data: (9) […]
0: Object { code: "1007", id: 20, is_free_zone: true, … }
1: Object { code: "1002", id: 15, is_free_zone: false, … }
2: Object { code: "1001", id: 14, is_free_zone: false, … }
3: Object { code: "1003", id: 16, is_free_zone: false, … }
4: Object { code: "1004", id: 17, is_free_zone: false, … }
5: Object { code: "1006", id: 19, is_free_zone: false, … }
6: Object { code: "1005", id: 18, is_free_zone: false, … }
7: Object { code: "1008", id: 21, is_free_zone: false, … }
8: Object { code: "1009", id: 22, is_free_zone: false, … }
length: 9
解决方案
见https://datatables.net/examples/ajax/objects.html
每列应该是一个单独的对象,其数据属性指向记录上的属性名称。
columns: [
{"data": "id"},
{"data": "name"},
{"data": "code"}
]
推荐阅读
- javascript - youtube 嵌入式 url 设置不使视频自动播放
- android - 上网恢复后收不到数据
- python - pythonic方法来计算列表/集合中的单词出现在数据框列中的次数
- windows - Windows RPC 远程中止关机
- swift - For Loop Swift 中的延迟
- php - Imagick::transformImage() 怎么突然“未定义”?
- c++ - 使用 Boost Spirit X3 和 Fusion 解析具有单个成员的结构变体
- spotify - 从 Spotify API 保存用户数据
- php - 使用严格属性类型 PDO::FETCH_CLASS 时处理空值
- java - 如何在解析期间执行电子邮件类型验证(OpenCSV,spring boot)?