我有一个用 Laravel 和 Lighthouse-php(用于 GraphQL)构建的 API。我的客户端是用 Vue js 构建的,并使用 Apollo 进行 graphQL 客户端实现。每当我提出请求时,我都会收到以下错误:
Access to fetch at 'http://localhost:8000/graphql' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
自然地,我继续安装 laravel-cors 包,但后来我意识到它是默认安装在我的 Laravel 安装(7.2.2)中的。这意味着它\Fruitcake\Cors\HandleCors::class
已经添加到中间件数组中Kernel.php
,并且 cors 配置文件已经在我的配置目录中。
经过一番谷歌搜索,我意识到我需要添加\Fruitcake\Cors\HandleCors::class
到我的文件中的route.middleware
数组config/lighthouse.php
它仍然没有工作。我已经重新启动服务器,清除缓存,清除配置并运行,composer dump-autoload
但我仍然得到错误。我不知道如何克服这一点。任何帮助将不胜感激。
版本
Laravel 7.2.2
Laravel 灯塔 4.10