[英]How to check if the ajax call for jQuery DataTable has returned data
我正在尝试从 AJAX 调用中加载 jQuery 数据表,如下面的代码所示。 但是我需要知道 DataTable 是否有回调来检查 AJAX 调用在加载表之前是成功返回还是失败。
$(function() {
$('#data-table').DataTable({
destroy: true,
responsive: true,
serverSide: false,
autoWidth: false,
paging: true,
filter: true,
searching: true,
stateSave: true,
scrollX: true,
lengthMenu: [5, 10, 25, 50, 75, 100],
ajax: {
url: 'https://jsonplaceholder.typicode.com/todos',
type: 'GET',
dataSrc: ''
},
columns: [{
title: 'Zone',
data: 'LastKnownZone',
}, {
title: 'Hiérarchie Map',
data: 'MapInfo.mapHierarchyString',
}, {
title: 'Addresse MAC',
data: 'macAddress',
}],
initComplete: function(json) {
let returned_data = json;
//..Do something with data returned
}
});
});
感谢任何帮助。
正如他们的官方网站上提到的:
为了完整我们的 Ajax 加载讨论,值得说明的是,目前 DataTables 不支持通过 Ajax 进行配置。 这是将来会审查的内容
但是你可以使用datasrc的思想,像这样:
$(function() {
$('#data-table').DataTable({
...
ajax: {
...
dataSrc: function ( receivedData ) {
console.log('The data has arrived'); // Here you can know that the data has been received
return receivedData;
}
},
...
});
});
只需在@Fawaz Ibrahim 的答案中添加一些内容,最好在 Ajax 调用中添加错误选项,以检查您是否遇到任何错误或问题,因为如果出现错误,dataSrc 回调将不会运行,因此您不会有任何成功返回的数据。
ajax: {
...
dataSrc: function ( receivedData ) {
console.log('The data has arrived'); // Here you can know that the data has been received
return receivedData;
},
error: function (xhr, error, thrown) {
console.log('here you can track the error');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.