Javascript 为 jquery 数据表 ajax 调用设置错误处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28050091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:09:29  来源:igfitidea点击:

set an error handler for jquery datatables ajax call

javascriptjquery-datatables

提问by aaron

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.

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

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

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

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

$.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' );} );

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.

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

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

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

回答by s0ndeb0k

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

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

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

See docs here:
https://datatables.net/reference/event/error
https://cdn.datatables.net/1.10.5/

请参阅此处的文档:
https: //datatables.net/reference/event/error
https://cdn.datatables.net/1.10.5/

回答by Muhammad Yasir

Alternatively use error function in ajax to log errors

或者在 ajax 中使用错误函数来记录错误

$('#table').DataTable({
        ajax: {
           dataType: "JSON",  
            type: "POST",
            url: url,
            data: [],
            async: true,
            error: function (xhr, error, code)
            {
                console.log(xhr);
                console.log(code);
            }
        },...

回答by J.C. Gras

Use the event as a custom error handler:

将该事件用作自定义错误处理程序

$(document).ready(function () {
    $('#myTable').on('error.dt', function (e, settings, techNote, message) {
        console.log('An error has been reported by DataTables: ', message);
    }).DataTable({
        "displayLength": 15,
        "ajax": {
          ....

回答by Evans L.

I am using Datatables 1.10.19 + Bootstrap and most of the solutions provided (including the accepted one above) did not work. However, I managed to capture the error as follows:

我正在使用 Datatables 1.10.19 + Bootstrap 并且提供的大多数解决方案(包括上面接受的解决方案)都不起作用。但是,我设法按如下方式捕获错误:

 "ajax" : {
    "datatype" : "json",
    "contentType" : "application/json",
    "method" : "GET",
    "url" : $url,
    "data" : { x : y },
    "dataSrc": function (data) {
      if (data.result = "OK"){
        return data.yourObj;
      }else{
        // hide processing or any loading modal here
        // display error on page or something
        console.log("Error: " + parseResultData(data.resultData));
        data.yourObj = [] //since datatables will be checking for the object as array
        return data.yourObj;
      }                           
    } 
  }