【问题标题】:Set cors headers in Laravel but still receiving error在 Laravel 中设置 cors 标头但仍然收到错误
【发布时间】:2016-12-25 22:12:39
【问题描述】:

我在 Laravel 中设置了 Cors 标头,但仍然收到错误消息。出于调试目的,我直接在 public/index.php 中设置它而不是在中间件中。我可以看到标题在那里,但仍然有错误。

让它更加混乱,每当我在 public/index.php 上设置它进行调试时,它都会给我第二个错误。但是,每当我将其注释掉时,这意味着根本没有标题,它给了我第一个错误。为什么?

这是我在 public/index.php 中设置的用于调试的:

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

【问题讨论】:

  • 你能分享你的cors.php
  • 我没有这个

标签: laravel cors


【解决方案1】:

试试这个,

=================================================
step 1 : install package
=================================================
composer require barryvdh/laravel-cors

=================================================
step 2 : config/app.php -> destination
=================================================

Barryvdh\Cors\ServiceProvider::class,

=================================================
step 3 : app/Http/Kernel.php -> destination
=================================================
protected $middleware = [
    // ...
    \Barryvdh\Cors\HandleCors::class,
];

=================================================
step 4 : Vendor publish
=================================================

php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 2017-08-04
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2019-08-11
    • 2019-09-17
    • 2019-10-22
    • 2021-09-21
    相关资源
    最近更新 更多