如何修复“响应中‘Access-Control-Allow-Origin’标头的值不能是通配符‘*’”

Kev*_*ndo 8 javascript php cors laravel reactjs

我正在开发一个项目,我使用 ReactJs 作为前端,使用 Laravel 7 作为后端。

显然,即使我在 Laravel 中配置了 Cors,'Allow-Origin'响应中的标头仍然返回"*",每次我使用 axios 发出请求时都会收到此错误"The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'",是的,我使用 withCredentials,因为在 Laravel 文档中它说我们必须允许凭据。

PS:请不要将其标记为重复,我检查了之前的问题,我们的问题有点不同,我找不到有用的答案

这是我的 config/cors.php 文件

'paths' => ['api/*', 'sanctum/csrf-cookie', '/login'],

'allowed_methods' => ['*'],

'allowed_origins' => ['http://127.0.0.1:3000'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => true,

 ];
Run Code Online (Sandbox Code Playgroud)

这是中间件 Cors.php 文件

public function handle($request, Closure $next)
{
        return $next($request)
        ->header('Access-Control-Allow-Origin', 'http://127.0.0.1:3000')
        ->header('Access-Control-Allow-Methods', "PUT, POST, GET, DELETE, OPTIONS")
        ->header('Access-Control-Allow-Headers', "Accept, Authorization, Content-Type")
        ->header('Access-Control-Allow-Credentials', "true");
}
Run Code Online (Sandbox Code Playgroud)

小智 -1

可能的原因:

  • 网页访问
  • 使用 Apache 对标头进行 CORS 授权
  • Laravel 缓存