很多时候401“未经授权。”对ajax请求

我的一个应用程序页面是通过我的页面中的ajax加载一些内容(目前有两个请求,文档就绪后)。 很多次我收到这个ajax请求状态“401”,响应“未授权”。 有时刷新页面(使用F5)是否正常工作,有时候一个请求正在接收401状态。 而且我收到500的次数较少(在这种情况下,laravel使用错误的数据库凭据,而不是来自.env)。

任何人都可以帮我解决这个问题吗?

使用Laravel 5.1.6

谢谢

public function handle($request, Closure $next) { if ($this->auth->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('auth/login'); } } return $next($request); } 

您可以尝试使用’|| $ request-> wantsJson()’用if检查请求是否为ajax。

 if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('auth/login'); } 

这是由于您的登录会话。 每当你的会话到期时。 请求响应是“未经授权”。