没有'Access

编程入门 行业动态 更新时间:2024-10-28 12:18:34
本文介绍了没有'Access-Control-Allow-Origin'标头-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

XMLHttpRequest cannot load 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 '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!

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

推荐答案

如果您使用的是 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

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

You also need to add Cors\ServiceProvider to your config/app.php providers array:

FruitCake\Cors\CorsServiceProvider::class,

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

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

供全球使用:

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

对于中间件使用:

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

步骤3

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

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

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

希望这个答案可以帮助某人面对与我自己相同的问题。

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

更多推荐

没有'Access

本文发布于:2023-11-24 19:11:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!