gpt4 book ai didi

reactjs - 带有水果蛋糕的 Laravel CORS

转载 作者:行者123 更新时间:2023-12-03 14:23:23 25 4
gpt4 key购买 nike

我做 react 项目与 laravel 后端...我有一个 CORS 问题,我做的一切都像下面的链接,水果蛋糕。
Laravel 6 CORS policy issue with API
但仍然无法正常工作。
cors.php:

        'paths' => ['api/*'],

/*
* Matches the request method. `[*]` allows all methods.
*/
'allowed_methods' => ['*'],

/*
* Matches the request origin. `[*]` allows all origins.
*/
'allowed_origins' => ['*'],

/*
* Matches the request origin with, similar to `Request::is()`
*/
'allowed_origins_patterns' => [],

/*
* Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
*/
'allowed_headers' => ['*'],

/*
* Sets the Access-Control-Expose-Headers response header.
*/
'exposed_headers' => false,

/*
* Sets the Access-Control-Max-Age response header.
*/
'max_age' => false,

/*
* Sets the Access-Control-Allow-Credentials header.
*/
'supports_credentials' => false,
而且,内核中间件是:
        protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

\Fruitcake\Cors\HandleCors::class,
];
还有什么问题?

最佳答案

以下是使用 fruitcake/laravel-cors 时的一些问题:

  • HandleCors $middleware顶部的中间件在 app/Http/Kernel.php :

  • protected $middleware = [
    \Fruitcake\Cors\HandleCors::class,
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];
    把它放在底部或中间的某个地方是行不通的,因为请求可能会被其他具有更高优先级的中间件拒绝。
  • 不是 在 Controller 中死亡或退出。

  • 例如,以下将不起作用:
    Route::get('/cors-test', function() {
    dd("This won't work");
    });
    因为 Fruitcake\Cors\HandleCors::handle方法添加相关标题 处理请求:
    Fruitcake\Cors\HandleCors.php
    public function handle($request, Closure $next)
    {
    // --- omitted

    // Handle the request
    $response = $next($request); // <--- if you die here

    if ($request->getMethod() === 'OPTIONS') {
    $this->cors->varyHeader($response, 'Access-Control-Request-Method');
    }

    // you will never reach here
    return $this->addHeaders($request, $response);
    }
    dump也不起作用
  • 更改后清除配置缓存 app/config/cors.php :
  • $ php artisan config:cache

    关于reactjs - 带有水果蛋糕的 Laravel CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168052/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com