Laravel 自定义中间件解决跨域问题 header 头不生效,有人解决过此类问题吗?
中间件核心代码如下:
public function handle($request, Closure $next)
{
$response = $next($request);
//允许请求来自哪些域名
$response->header('Access-Control-Allow-Origin', '*');
//允许请求中包含哪些header
$response->header('Access-Control-Allow-Headers', 'token,Origin, X-Requested-With, Content-Type, Accept');
//允许请求采用哪些请求方式
$response->header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
//允许请求中携带cookie
$response->header('Access-Control-Allow-Credentials', 'true');
//自定义的请求头token
$response->header('Access-Control-Expose-Headers', 'token');
return $response;
}
问题描述:
添加了header头 token时 报不允许Origin
不添加header头 token时 不同域名之间接口可以正常请求
推荐文章: