Mic*_*kis 0 ajax jquery datatables
我正在使用DataTables:
$t = $('#users-table').DataTable({
'paging' : true,
'searching' : true,
'ordering' : false,
'info' : false,
'serverSide': true,
'processing': true,
'ajax' : {
'url' : 'http://localhost/api/users',
'type' : 'GET',
'dataSrc' : 'users',
'error': function() {
// how can i handle the 404 response and show the no results found message?
}
}
...
Run Code Online (Sandbox Code Playgroud)
如果查询没有结果,服务器将返回404未找到的响应.我正在尝试显示无结果消息并从表中删除当前数据,但相反,数据将在表中保持相同的错误响应.
编辑:
我正在使用这样的搜索功能:
$('#data-filter').keyup(function() {
$t.search($(this).val()).draw();
});
Run Code Online (Sandbox Code Playgroud)
因此,如果未找到查询字的结果,则返回404.
重要
404没有结果时返回错误几乎没有任何意义.看到这个优秀的答案,原因.
编写脚本以将空数组作为data参数值返回并指示没有result(recordsFiltered)和结果集(recordsTotal)的总大小会更好,有关详细信息,请参阅返回的数据.
替代方法
但是,您可以使用ajax以下概述的选项来禁止显示警报消息并清除错误表:
"ajax": {
'url': 'http://localhost/api/users',
'type': 'GET',
'dataSrc': 'users',
'error': function(jqXHR, textStatus, errorThrown){
$('#example').DataTable().clear().draw();
}
},
Run Code Online (Sandbox Code Playgroud)
DEMO
有关代码和演示,请参阅此jsFiddle.