【发布时间】:2020-01-05 16:53:18
【问题描述】:
我在 Angular 中有一个拦截器,用于将 Angular 发送到 Laravel CORS
let headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/x-www-form-urlencoded',
'Authorization': localStorage.getItem('token'),
});
let reqClone = req.clone({
headers: headers
});
在 Laravel 的中间件中
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Credentials', 'true')
->header('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS, POST, PUT')
->header('Access-Control-Max-Age', '3600')
->header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With');
我尝试访问
$request->header('authorization') // Laravel
但我收到与 CORS 相关的错误
来自原点“http://localhost:4200”已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头
【问题讨论】:
-
您需要将授权添加到您的 Allow-Headers..
-
你可以看看例子,好吗?它放在哪里。
-
抱歉,在移动端。 ->header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With', 'Authorization');
-
我会试试的,谢谢你的帮助