heroku Laravel生产,CORS无“访问控制-允许-来源”标题

ddarikpa  于 2022-11-13  发布在  其他
关注(0)|答案(3)|浏览(131)

在Heroku上部署了我的Laravel API应用程序后,它似乎在Cors上出现了问题。这在开发过程中没有问题,我仍然可以在postman上使用已部署的应用程序,但不能通过Angular应用程序上的常规HTTP请求。

您访问的页面没有被修改!

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

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => ['*'],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

我的Kernel.php使用了HandleCors类

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

不确定我的angular应用程序是否需要进一步更改,因为它在postman上工作,调用看起来像这样

// User registration
  register(user: User): Observable<any> {
    return this.http.post('https://secure-fortress-48055.herokuapp.com/api/register', user);
  }

有什么解决办法吗?

voase2hg

voase2hg1#

在你的Cors.php文件中修改它。

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

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,
dsekswqp

dsekswqp2#

你可以发布你的.env文件吗?我认为你需要修改你的.env文件,并更改下面的代码

APP_URL=http://localhost

APP_URL=https://secure-fortress-48055.herokuapp.com/

您还需要更改Cors.php

'exposed_headers' => [],

'exposed_headers' => ['Access-Control-Allow-Origin'],

也许这个能帮到你

ny6fqffe

ny6fqffe3#

我通过在api.php路由中添加头来解决这个问题

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, PATCH, DELETE');
header('Access-Control-Allow-Headers: Accept, Content-Type, X-Auth-Token, Origin, Authorization');

也许不是最优雅的解决方案,但它是一个。

相关问题