用于子域API的Laravel CORS [英] Laravel CORS for subdomain API
问题描述
我正在使用Laravel使用Laravel提供GraphQL API。当我在主域中使用grapqhl作为URI时,一切正常,但是在子域中使用它时,例如。 grapql.app.test
我遇到下一个错误:
I'm using Laravel to serve a GraphQL API, using Lighthouse. Everything works fine when I use grapqhl as a URI in the main domain, but when I use it in a subdomain eg. grapql.app.test
I get the next error:
在'http:/ /graphql.app.test/'源自原始
'http://app.test'已被CORS政策阻止:对
飞行前请求的响应未通过访问控制检查:否
'访问控制允许来源'
Access to fetch at 'http://graphql.app.test/' from origin 'http://app.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'
我的 config\cors
:
'paths' => ['api/*','graphql'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
我该如何解决?
我正在使用laravel 8
I'm using laravel 8.
推荐答案
如果有人对此有疑问,则需要添加将要使用的路径。在我的情况下是 /
,因为我的API在 grapql.app.test
中。
If anyone is having trouble with this, you need to add the paths that you will use. In my case was /
due that my API is in grapql.app.test
.
结果代码:
'paths' => ['api/*','graphql','/'],
'allowed_methods' => ['*'],
'allowed_origins' => ['http://app.test'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
这篇关于用于子域API的Laravel CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!