我为这个问题苦苦挣扎了 5 个多小时。所以请帮忙:)
我的前端堆栈是 Angularjs
& 我的后端堆栈是 LAMP
和 Laravel
框架。
我遇到的问题是,当我想在 ui-router
解析器中使用 $http
服务发送 GET
请求作为依赖项,firefox 抛出错误:同源策略不允许读取远程资源
但我已经使用 barryvdh/laravel-cors
包配置了我的后端,以便使用其中间件来设置正确的 header 。
Angular 代码片段:
$http.post('http://example.com', data)
.then(function (data, status, headers, config) {
},
function (data, status, headers, config) {
});
Laravel 代码片段:
//controller constructor
public function __construct()
{
//parent::__construct();
$this->middleware('cors');
}
这是一个非常简单和正常的代码,我不知道为什么它不起作用。
这是我的 HTTP 请求/响应日志:
我不知道为什么 CORS header 不存在,尽管它们存在于服务器日志中。
最佳答案
您必须检查您的服务器是否发送该 header : 访问控制允许来源“*”
如果您的服务器不发送它,您的浏览器在打开与另一个域的连接时将会失败。
如果您使用 grunt,您的另一个选择是使用 grunt-connect-proxy 插件。
检查此链接。 http://www.hierax.org/2014/01/grunt-proxy-setup-for-yeoman.html
关于javascript - Angular/Laravel CORS 问题 : The Same Origin Policy disallows reading the remote resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088085/