没有“Access-Control-Allow-Origin"标头 - Laravel

No #39;Access-Control-Allow-Origin#39; header - Laravel(没有“Access-Control-Allow-Origin标头 - Laravel)
本文介绍了没有“Access-Control-Allow-Origin"标头 - Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

XMLHttpRequest 无法加载 http://myapi/api/rating.对预检请求的响应未通过访问控制检查:请求的资源上不存在Access-Control-Allow-Origin"标头.Origin 'http://localhost:8104' 因此不允许访问.响应的 HTTP 状态代码为 403.

XMLHttpRequest cannot load http://myapi/api/rating. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8104' is therefore not allowed access. The response had HTTP status code 403.

我不明白为什么我不能发出 CORS 请求.我已经在这里安装了中间件,将它添加到全局http内核中,但它仍然不起作用.尝试根据 stackoverflow 建议创建自定义中间件,但这也不起作用.还尝试添加一个路由组.最后,我尝试在请求操作中手动设置响应标头.我真的被困住了 - 感谢帮助!

I can't figure out why I can't make CORS requests. I've install the middleware here, added it to the global http kernel, but it still doesn't work. Tried to create a custom middleware given stackoverflow suggestions but that also did not work. Also tried adding a Route group. Lastly, I tried setting the response headers manually in the request action. I'm really stuck - help is appreciated!

查看代码:https://gist.github.com/KerryRitter/0d7ababb7b9eb8d54f0ae55

推荐答案

如果你使用的是 Laravel 5.5 &Laravel 5.x 并面临同样的问题,如请求的资源上不存在Access-Control-Allow-Origin"标头.只需使用以下软件包并配置您的系统.

If you are using Laravel 5.5 & Laravel 5.x and facing same problem like No 'Access-Control-Allow-Origin' header is present on the requested resource. Just use following package and config your system.

第 1 步:

composer require barryvdh/laravel-cors

第 2 步

您还需要将 CorsServiceProvider 添加到您的 config/app.php providers 数组:

You also need to add CorsServiceProvider to your config/app.php providers array:

FruitCakeCorsCorsServiceProvider::class,

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

To allow CORS for all your routes, add the HandleCors middleware in the $middleware property of app/Http/Kernel.php class:

供全球使用:

protected $middleware = [
    // ...
    FruitcakeCorsHandleCors::class,
];

中间件用途:

protected $middlewareGroups = [
   'web' => [
       // ...
   ],

   'api' => [
        // ...
        FruitcakeCorsHandleCors::class,
    ],
];

第三步

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

Once your installation completed run below command to publish the vendor files.

php artisan vendor:publish --provider="FruitcakeCorsServiceProvider"

希望这个回答能帮助和我一样面临同样问题的人.

Hope this answer helps someone facing the same problem as myself.

这篇关于没有“Access-Control-Allow-Origin"标头 - Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)