问题描述
我的Vue,Symfony(使用Docker进行安装)项目在使用CORS策略时出现问题,该项目阻止了我对API的请求,因此我添加了nelmio-cors-bundle并进行了设置,因此现在可以正常响应了。
这是我的nelmio配置:
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET','OPTIONS','POST','PUT','PATCH','DELETE']
allow_headers: ['Content-Type','Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
这是我的控制器:
/**
* @Route("/movement-types")
*/
public function getMovementTypes(MovementTypeRepository $repo): Response
{
$items = $repo->findAll();
$response = new Response();
$response->setContent(json_encode($items));
return $response;
}
到目前为止,这可行。但是每当我将var_dump()函数的dump()添加到控制器时,CORS策略都会再次阻止它。我试图将nelmio配置更改为尽可能包容性最高的设置,但它也不起作用。
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_origin: ['*']
allow_methods: ['*']
allow_headers: ['*']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
即使我认为这不是最好的解决方案,我也尝试过此方法。 https://github.com/fruitcake/laravel-cors/issues/301但它也不起作用。 我没有找到有关此主题的任何其他解决方案或讨论。
感谢您的任何帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)