在中间件中使用return
返回信息给客户端,laravel会报错,显示headers
是一个空的对象
if (!$res){
if(request()->ajax()){
return json_encode(['code'=>0,'message'=>'未登录授权,请联系管理员']);
}else{
redirect('/a/noauth')->send();
}
}
解决办法:使用echo
方法返回数据,并且使用exit;
跳出中间件
if (!$res){
if(request()->ajax()){
echo json_encode(['code'=>0,'message'=>'未登录授权,请联系管理员']);exit;
}else{
redirect('/a/noauth')->send();
}
}
记得别漏掉exit;