简体   繁体   English

意外访问Access-Control-Allow-Origin标头错误

[英]Unexpected access Access-Control-Allow-Origin header error

I have been trying to understand this for a while and just can't get it! 我一直试图理解这一点,但是无法得到它!

I followed a tutorial ( https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps ) but instead of putting Angular inside the /public folder, I created an external client. 我按照教程( https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps ),但不是将Angular放在/ public文件夹中,而是创建了一个外部客户端。

Using MAMP PRO, I put the API is at http://www.jotbot.local and the client is at http://www.jotclient.local . 使用MAMP PRO,我把API放在http://www.jotbot.local ,客户端在http://www.jotclient.local When I try to login, it get this error: 当我尝试登录时,会收到此错误:

XMLHttpRequest cannot load http://www.jotbot.local/api/authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.jotclient.local' is therefore not allowed access.

In the CORS config in Laravel I have: 在Laravel的CORS配置中,我有:

return [ 
    'supportsCredentials' => false, 
    'allowedOrigins' => ['*'], 
    'allowedHeaders' => ['*'], 
    'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'], 
    'exposedHeaders' => ['*'], 
    'maxAge' => 0, 
    'hosts' => [], 
];

I am not sure WHAT to change. 我不知道要改变什么。 Is the 'allowedOrigins' => ['*'] line or the 'hosts' => [] line what needs to be changed? 'allowedOrigins'=> ['*']行或'hosts'=> []行是否需要更改?

Also, the client (in my real app) will be publicly accessed and will be given to a client whenever he is processed meaning I won't know the domains I need to allow so I need SOME KIND of wildcard, so I am really confused and documentation on the CORS package is hard to find (or I don't know what I am looking for ... LOL) 此外,客户端(在我的真实应用程序中)将被公开访问,并且每当他被处理时将被提供给客户端意味着我将不知道我需要允许的域名因此我需要一些通配符,所以我真的很困惑很难找到关于CORS包的文档(或者我不知道我在寻找什么......大声笑)

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

I followed that same tutorial and faced the same problem. 我遵循相同的教程并面临同样的问题。

You have to add the CORS for Laravel 5.1. 你必须为Laravel 5.1添加CORS。

Follow the steps on installation and it will work fine. 按照安装步骤操作,它将正常工作。

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

Require the barryvdh/laravel-cors package in your composer.json and update your dependencies. 需要composer.json中的barryvdh / laravel-cors包并更新您的依赖项。

$ composer require barryvdh/laravel-cors 0.7.x

Add the Cors\\ServiceProvider to your config/app.php providers array: Cors \\ ServiceProvider添加到config / app.php提供程序数组:

 Barryvdh\Cors\ServiceProvider::class,

On your routes.php file add the middleware routes.php文件中添加中间件

Route::group(['prefix' => 'api','middleware' => 'cors'], function()

Or 要么

 Route::group(['middleware' => 'cors'], function()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM