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
发布评论