我无法使DataTable加载通过API调用获得的新数据。
错误显示为:
DataTables警告:表id =从属-无法重新初始化DataTable。有关此错误的更多信息,请参见http://datatables.net/tn/3
DataTables警告:表id = slave-请求第0行第0列的未知参数'0'。有关此错误的更多信息,请参见http://datatables.net/tn/4
这是我的Ajax代码:
$(document).ready( function (){
$('#slave').dataTable()
});
function updateDatTable() {
fetch('{{route('pusher')}}', {
method : 'post',
mode: 'cors',
headers: {
'Content-Type': 'application/json', // sent request
'Accept': 'application/json' // expected data sent back
},
body: JSON.stringify({"mac":"{{$slaves['mac']}}"})
})
.then((res) => res.json())
.then(function(res) {
var temp = res['slaves'];
var table = $('#slave').DataTable({
"data":temp,
"columns":[
{ data: "name" },
{ data: "slave_id" },
{ data: "type" },
{ data: "status" },
{ data: "value"},
{ data: "mode"},
{ data: "name1"},
{ data: "name2"},
{ data: "name3"},
{ data: "name4"},
{ data: "s1"},
{ data: "s2"},
{ data: "s3"},
{ data: "s4"},
{ data: "voltage"},
{ data: "hum"},
{ data: "temp"}
]
});
table.clear().rows.add(temp).draw();
alert('yay');
//setInterval(updateDatTable, 1000);
})
.catch(function(error) {
alert(error)
//setInterval(updateDatTable, 1000); // <-- there was a network problem,
// but still, program the next one!
})
}
我通过进行API调用获得的数据如下:
{
"slaves": [
{"name":"IPL Conf Room Door","type":"door_sensor","value":0,"slave_id":3,"status":1,"voltage":2},
{"slave_name":"IPL Motion Sensor","name":"IPL Motion Sensor","type":"PIR","value":1,"slave_id":4,"status":1,"voltage":2},
{"hum":83,"temp":22,"name":"IPL Conf Room Extension","type":"ir_relay","slave_id":5,"status":1,"voltage":10},
{"mode":4,"name":"IPL Scene Switch","type":"macro_panel","slave_id":6,"status":1,"voltage":0},
{"mode":3,"s3":1,"name3":"","name":"IPL Conf Lights","type":"fb","name2":"","name1":"","s1":1,"slave_id":7,"s2":1,"status":1},
{"hum":84,"temp":23,"name":"IPL Reception Extension","type":"ir_relay","slave_id":9,"status":1,"voltage":10},
{"name":"IPL Main Door","type":"door_sensor","value":1,"slave_id":10,"status":1,"voltage":2}
],
"amount":7,
"ack":"ok",
"action":"list_slave",
"type":"all",
"mac":"C8EEA62DA254"
}
请帮助。如果您需要更多信息,请询问。