为 jquery 数据表 ajax 调用设置错误处理程序 [英] set an error handler for jquery datatables ajax call

查看:30
本文介绍了为 jquery 数据表 ajax 调用设置错误处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ajax 调用中出现问题(即服务器没有响应)时使用自定义错误处理程序,以便将新数据加载到我的数据表中.

I'm trying to have a custom error handler when something goes wrong (i.e. the server does not respond) on the ajax call for loading new data into my datatable.

$table.DataTable().ajax.url(ajaxURL).load();

默认情况下,它显示一个警报,我可以更改它以使用以下设置引发 javascript 错误:

By default it shows an alert and I can change that to throw a javascript error with the following setting:

$.fn.dataTable.ext.errMode = 'throw';

但是有了这个,我只是在控制台记录了一个错误,我不知道如何捕捉这个抛出的错误,所以我仍然无法提供我自己的错误处理程序.

But with this, I just have an error logged to the console and I'm not sure how to catch that thrown error so I still can't provide my own error handler.

文档中还列出了一个错误事件,但似乎没有触发,因此以下永远不会发出警报.

There is also an error event listed in the documentation, but that doesn't seem to get triggered, so the following never alerts.

$table.on( 'error', function () { alert( 'error' );} );

到目前为止我发现的所有其他内容都是针对遗留代码的,例如设置 fnServerData,我想避免进入.

Everything else I've found so far is for the legacy code, such as setting the fnServerData, which I would like to avoid getting into.

1.10 API有没有设置ajax错误回调的方法?

Is there a method to set the ajax error callback in the 1.10 API?

推荐答案

Datatables v1.10.5(2015 年 2 月 10 日发布)中添加了新的错误事件处理.

New error event handling has been added in Datatables v1.10.5 (released 10th February 2015).

$.fn.dataTable.ext.errMode = function ( settings, helpPage, message ) { 
    console.log(message);
};

在此处查看文档:
https://datatables.net/reference/event/error
https://cdn.datatables.net/1.10.5/

这篇关于为 jquery 数据表 ajax 调用设置错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆