首页 文章

没有'Access-Control-Allow-Origin' Headers - Laravel 5.4

提问于
浏览
3

XMLHttpRequest无法加载http://myapi/api/rating . 因此,不允许对预检请求进行响应,因此不允许访问 - 控制 - 允许 - 来源' header is present on the requested resource. Origin ' http://localhost:8104 . 响应具有HTTP状态代码403 .

我无法弄清楚为什么我不能提出CORS请求 . 我在这里安装了中间件,将其添加到全局http内核中,但它仍然不起作用 . 试图创建一个自定义中间件给出stackoverflow建议,但也没有用 . 还尝试添加路由组 . 最后,我尝试在请求操作中手动设置响应标头 . 我真的被卡住了 - 感谢帮助!

参见代码:https://gist.github.com/KerryRitter/0d7ababb7b9eb8d54f0ae55add9704a1

5 回答

  • 4

    https://github.com/barryvdh/laravel-cors

    laravel-cors包允许您使用Laravel中间件配置发送跨源资源共享头 .

    特征

    处理CORS飞行前OPTIONS请求将CORS标头添加到您的响应中

  • 1

    如果你正在使用 Laravel 5.5Laravel 5.x 并面临同样的问题,如 No 'Access-Control-Allow-Origin' header is present on the requested resource . 只需使用以下包并配置您的系统 .

    步骤1:

    composer require barryvdh/laravel-cors
    

    第2步

    您还需要将 Cors\ServiceProvider 添加到 config/app.php 提供程序数组中:

    Barryvdh\Cors\ServiceProvider::class,
    

    要允许 CORS 用于所有路由,请在 app/Http/Kernel.php 类的 $middleware 属性中添加 HandleCors 中间件:

    适用于全球用途:

    protected $middleware = [
        // ...
        \Barryvdh\Cors\HandleCors::class,
    ];
    

    对于中间件用途:

    protected $middlewareGroups = [
       'web' => [
           // ...
       ],
    
       'api' => [
            // ...
            \Barryvdh\Cors\HandleCors::class,
        ],
    ];
    

    第3步

    安装完成后,运行以下命令以发布供应商文件 .

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

    希望这个答案可以帮助那些面临同样问题的人 .

  • 0

    我最近在laravel 5.4中遇到了这个错误,我发送ajax post请求到我自己的网站,并且仍然得到同样的错误,我由于两个原因准确地面对这个错误,

    error: XMLHttpRequest cannot load https://[mydomain].com/quote/short. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://[mydomain].com' is therefore not allowed access.

    上述错误的原因是,我是从https域向http域发布请求,所以当我将其更改为https时,错误已解决,然后由于类似的原因再次出现同样的错误,这就是原因,这个时间,领域有 www. 并且请求的没有,在我添加了 www. 之后,它就像一个魅力 .

    对于跨源请求,我习惯于遵循以下解决方案:

    • 创建一个中间件(在我的情况下是cors),代码如下
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    
    • 将中间件插入kernal.php中的routeMiddleware数组中

    'cors' => \App\Http\Middleware\Cors::class,

    • 将中间件添加到受尊重的路由

    Route::get('myRoute', ['middleware' => 'cors' , 'uses'=> 'MyController@Action']

    希望这个答案可以帮助那些面临同样问题的人 .

  • 7

    您可以创建Cors中间件类并添加到应用程序中's global HTTP middleware stack in kenel.php. Middlewares in this stack will run during every request to your application. After adding middleware into that stack you don' t想要在api.php文件中运行它 . 请按照this链接获取更多信息 .

  • 0

    由于安全问题,Laravel默认限制交叉源请求 . 我们需要创建一个Cors中间件来接受来自不同来源的请求 .

    第1步:创建Cors中间件 .

    php artisan make:middleware Cors
    

    第2步:在返回之前在句柄函数中添加以下行 .

    //header('Access-Control-Allow-Origin:  *');
    header('Access-Control-Allow-Origin:  http://localhost:4200');
    header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Authorization, Origin');
    header('Access-Control-Allow-Methods:  POST, PUT');
    

    第3步:在app / Http / Kernel.php文件中注册middileware .

    Add below line in $middleware array 
    
    \App\Http\Middleware\Cors::class,
    

    第4步:现在我们必须在app / Http / Kernel.php文件中调用中间件

    Add below line in $routeMiddleware array 
    
    'cors' => \App\Http\Middleware\Cors::class,
    

相关问题