所以我遇到了常见错误“ DataTables警告:表id = myTable-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1”。我在网上尝试过的解决方案对我来说并不是很有效。所以基本上我正在测试本地JSON文件,并使用数据表ajax方法填充表。我似乎无法弄清楚我做错了什么。
$(document).ready(function () {
var request = new XMLHttpRequest();
request.open("GET", "./test.json", false);
request.send(null)
var responseMain = JSON.parse(request.responseText);
var my_array = responseMain.feed.entry;
$("#totalnum").html(my_array.length);
var obj_stage = [];
$.each(my_array, function (index, value) {
obj_stage.push(value.content.F_Form1);
console.log("inside each", obj_stage)
});
console.log("out side each", obj_stage)
console.log("what type",typeof obj_stage);
$('#myTable').DataTable({
"ajax": obj_stage,
"columns": [
{ "data": obj_stage["-flowState"] },
{ "data": obj_stage["-flowState"] },
]
});
});
//console.log("value",value.content.F_Form1["-flowState"])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.18/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
<script src="./test.js"></script>
</head>
<body>
<h1 id="totalnum"></h1>
<table id="myTable" class="display" style="width:100%">
<thead>
<tr>
<th>Stage Name</th>
<th>Asap ?</th>
</tr>
</thead>
</table>
</body>
</html>
答案 0 :(得分:1)
您可以直接调用该对象,因为该对象已经加载在obj_stage
中。
不需要这样做{ "data": obj_stage["-flowState"] },
,而是{ "data": "-flowState" },
,第一个将不会填充列,因为对象已经在数组中了(,并且只能使用其索引< / em>),DataTables将为您使用它的属性名称或键。
$(document).ready(function () {
var request = new XMLHttpRequest();
request.open("GET", "./test.json", false);
request.send(null)
var responseMain = JSON.parse(request.responseText);
var my_array = responseMain.feed.entry;
$("#totalnum").html(my_array.length);
var obj_stage = [];
$.each(my_array, function (index, value) {
obj_stage.push(value.content.F_Form1);
});
//if(my_array.length === obj_stage.length ){
$('#myTable').DataTable({
"data": obj_stage,
"columns": [
{ "data": "-flowState" },
{ "data": "-flowState" },
]
});
//}
});