Datatables 1.10.5 ajax错误处理程序——访问http状态码。

时间:2022-06-05 12:48:05

I'm using Datatables 1.10.5 and I have the ajax error handler defined. I need to gain access to the actual http status code when the error fires so I can see if my user's session time has expired (HTTP 401) vs if there's something wrong on the backend such as an HTTP 500 error. Right now the techNote is always 7.

我使用的是Datatables 1.10.5,并定义了ajax错误处理程序。当错误触发时,我需要访问实际的http状态代码,以便查看用户的会话时间是否已经过期(http 401),以及后端是否有问题,比如http 500错误。现在techNote总是7。

How can I get that elusive HTTP status code from the ajax transaction? I tried below, but it does not fire.

如何从ajax事务中获得难以捉摸的HTTP状态代码?我在下面试过了,但它不开火。

$("#example").ajaxError(function(event, jqxhr, request, settings){
    alert("Failure HTTP Code:"+jqxhr.status);    
});

and

$.fn.dataTable.ext.errMode = 'throw';
$('#example').on('error.dt', function(e, settings, techNote, message) {
   console.log( 'An error has been reported by DataTables: ', message);
});

Does not have the information I need, or at least that I cannot find it in any of the passed variables.

没有我需要的信息,或者至少在任何传递的变量中都找不到。

2 个解决方案

#1


2  

Handle xhr event. When Ajax error occurs third argument json would be null and fourth argument xhr would contain jQuery XHR object. You can get the status by accessing xhr.status property.

处理xhr事件。当出现Ajax错误时,第三个参数json为null,第四个参数xhr包含jQuery xhr对象。您可以通过访问xhr获得状态。状态属性。

Also see $.fn.dataTable.ext.errMode which could be used to instruct DataTables not to show the alert.

也看到.fn.dataTable.ext美元。错误模式,可以用来指示数据不能显示警报。

#2


1  

I've been able to get access to the status code without overriding global jQuery ajaxError by overriding the more specific to DataTables $.fn.dataTable.ext.errMode with a function:

通过覆盖更特定于DataTables $.fn. datatable.ext的全局jQuery ajaxError,我能够访问状态代码。errMode函数:

    $.fn.dataTable.ext.errMode = function (settings, tn, msg) {
      if (settings && settings.jqXHR && settings.jqXHR.status == 401) {
          window.location = window.location.origin + '/login';
          return
      }
      alert(msg) // Alert for all other error types; default DataTables behavior
    };

This example shows a redirect to login on 401 status code, however, you could do the same with any other status code.

此示例显示了在401状态码上的重定向登录,但是,您也可以对任何其他状态码进行重定向。

Last note is you might want to leverage the DataTables statusCode option for status code specific handling but you'll still need to override $.fn.dataTable.ext.errMode if you want to bypass default error handling since it executes before anything you define in statusCode

最后要注意的是,您可能希望利用DataTables statusCode选项进行状态代码特定的处理,但仍然需要覆盖$.fn. datatable.ext。如果您想绕过默认错误处理,errMode是在您在statusCode中定义的任何内容之前执行的

#1


2  

Handle xhr event. When Ajax error occurs third argument json would be null and fourth argument xhr would contain jQuery XHR object. You can get the status by accessing xhr.status property.

处理xhr事件。当出现Ajax错误时,第三个参数json为null,第四个参数xhr包含jQuery xhr对象。您可以通过访问xhr获得状态。状态属性。

Also see $.fn.dataTable.ext.errMode which could be used to instruct DataTables not to show the alert.

也看到.fn.dataTable.ext美元。错误模式,可以用来指示数据不能显示警报。

#2


1  

I've been able to get access to the status code without overriding global jQuery ajaxError by overriding the more specific to DataTables $.fn.dataTable.ext.errMode with a function:

通过覆盖更特定于DataTables $.fn. datatable.ext的全局jQuery ajaxError,我能够访问状态代码。errMode函数:

    $.fn.dataTable.ext.errMode = function (settings, tn, msg) {
      if (settings && settings.jqXHR && settings.jqXHR.status == 401) {
          window.location = window.location.origin + '/login';
          return
      }
      alert(msg) // Alert for all other error types; default DataTables behavior
    };

This example shows a redirect to login on 401 status code, however, you could do the same with any other status code.

此示例显示了在401状态码上的重定向登录,但是,您也可以对任何其他状态码进行重定向。

Last note is you might want to leverage the DataTables statusCode option for status code specific handling but you'll still need to override $.fn.dataTable.ext.errMode if you want to bypass default error handling since it executes before anything you define in statusCode

最后要注意的是,您可能希望利用DataTables statusCode选项进行状态代码特定的处理,但仍然需要覆盖$.fn. datatable.ext。如果您想绕过默认错误处理,errMode是在您在statusCode中定义的任何内容之前执行的