小编cra*_*dul的帖子

使用 Laravel Lighthouse 的 Laravel 7 中的 CORS 错误

我有一个用 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.
Run Code Online (Sandbox Code Playgroud)

自然地,我继续安装 laravel-cors 包,但后来我意识到它是我的 Laravel 安装(7.2.2)默认提供的。这意味着它\Fruitcake\Cors\HandleCors::class已经添加到中间件数组中Kernel.php,并且 cors 配置文件已经在我的配置目录中。

经过一些谷歌搜索,我意识到我需要添加\Fruitcake\Cors\HandleCors::classroute.middleware …

cors laravel graphql vue-apollo laravel-lighthouse

3
推荐指数
1
解决办法
1871
查看次数

标签 统计

cors ×1

graphql ×1

laravel ×1

laravel-lighthouse ×1

vue-apollo ×1