我正在尝试使用 Restangular在我的 AngularJS 应用程序中访问 REST API,我遇到了“Access-Control-Allow-Origin” header 的问题。我知道出于安全原因,这需要与 API 响应一起返回,并且它正在被返回,但由于某种原因XMLHttpRequest
似乎没有注意到它。
完整的错误消息是这样的:
XMLHttpRequest cannot load http://api.mysite.dev/users. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite.dev' is therefore not allowed access.
如果我在 Chrome 开发工具的“网络”选项卡中查看请求,我可以看到响应中设置了 header is:
这是我用来在 Laravel REST 应用程序中提供这些 header 的代码:
Route::options('{all}', function ()
{
$response = Response::make('');
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
$response->header('Access-Control-Allow-Headers', 'Accept, Content-Type');
return $response;
})
->where('all', '.*');
你知道这里可能出了什么问题吗?
最佳答案
尝试这些额外的 Access-Control-Allow-Headers:
$response->header('Access-Control-Allow-Headers', 'Authorization,content-type,accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since');
关于javascript - 使用 Restangular 时出现“Access-Control-Allow-Origin” header 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398622/