简体   繁体   English

Laravel DataTables处理401未经授权的错误

[英]Laravel DataTables handle 401 Unauthorized error

I inherited a Laravel application that is using Yajra DataTables to display grid content in a page. 我继承了一个使用Yajra DataTables在页面中显示网格内容的Laravel应用程序。

If Laravel's session expires, the DataTable will throw the following alert: 如果Laravel的会话到期,则DataTable将引发以下警报:

DataTables warning: table id=dataTableBuilder - Ajax error. DataTables警告:表格ID = dataTableBuilder-Ajax错误。 For more information about this error, please see http://datatables.net/tn/7 有关此错误的更多信息,请参见http://datatables.net/tn/7

Viewing the Network tab in Chrome shows me that the AJAX response for the DataTable was a 401 Unauthorized. 在Chrome浏览器中查看“网络”标签后,我发现DataTable的AJAX响应是“ 401未经授权”。 This is all expected behavior, but instead of displaying an arbitrary error to the user, I'd like to send the user to the login page with a message that says "your session timed out, please log in again." 这是所有预期的行为,但我不想向用户显示一个任意错误,而是希望将用户发送到登录页面,并显示一条消息:“您的会话已超时,请重新登录。” or something. 或者其他的东西。

I can't figure out how to do that. 我不知道该怎么做。

Traditional DataTables integrations allow me to pass an error handler to the AJAX response (ie {ajax: { error: function () {...} }} ), but there doesn't seem to be a way to do that with Laravel DataTables. 传统的DataTables集成允许我将错误处理程序传递给AJAX响应(即{ajax: { error: function () {...} }} ),但是Laravel DataTables似乎没有办法做到这一点。 。

Laravel DataTables has an html() method that I can override like so: Laravel DataTables有一个html()方法,我可以像这样重写:

public function html() {
    return $this->builder()
           ->ajax([
             error => ''
           ]);
}

But the ajax method on the builder doesn't allow me to pass a JavaScript handler to the error property. 但是构建器上的ajax方法不允许我将JavaScript处理程序传递给error属性。

Is there anyway to accomplish the force login when sessions time out? 会话超时时,是否有任何方法可以完成强制登录?

Given the comment chain above, my suggestion would be to put a unique string in the error response, like: 给定上面的注释链,我的建议是在错误响应中放入一个唯一的字符串,例如:

code: 401

Then, use the global $.ajaxError to catch errors and do something different based on the response text: 然后,使用全局$.ajaxError捕获错误并根据响应文本执行其他操作:

$( document ).ajaxError(function( event, jqxhr, settings, thrownError ) {
    if (typeof settings === "string" && settings.indexOf('401') > -1) {
        //you can probably do something here
    }
});

You could probably even do this in a more efficient way as the 401 response type should be observable in one of the 4 properties (can't remember which) 您甚至可能以更有效的方式执行此操作,因为401响应类型在4个属性之一中应该是可见的(不记得是哪个)

An option more specific to DataTables that would not necessitate a global override is to use a function for $.fn.dataTable.ext.errMode 对于DataTables更特定的,不需要全局覆盖的选项是对$.fn.dataTable.ext.errMode使用函数

    $.fn.dataTable.ext.errMode = function (settings, tn, msg) {
      if (settings && settings.jqXHR && settings.jqXHR.status == 401) {
         // Handling for 401 specifically
      }
      // Handling for all other errors, this implements the DataTables default 
      // behavior of throwing an alert
      alert(msg)
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM