angularjs – Restangular跨域请求.我做错了什么?

我的域sub.example.com配置了restangular:
RestangularProvider.setDefaultHeaders({
    'Content-Type': 'application/json','X-Requested-With': 'XMLHttpRequest'
});
RestangularProvider.setDefaultHttpFields({
    'withCredentials': true
});

然后我通过以下方式建造其他工厂:

return Restangular.withConfig(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('http://api.example.com');
});

并且,显然,获取错误否请求资源上存在“Access-Control-Allow-Origin”标头.因此,不允许来源“http://sub.example.com”访问..如何配置服务器/客户端以获取有效的跨域请求?

//更新

我在后端使用Yii并发送下一个标题
header(‘Access-Control-Allow-Origin:*’,true);

我找到了解决方案.

首先,当使用凭证时 – 我们不能使用*来访问Access-Control-Allow-Origin.
然后,XHR发送应该处理好的OPTIONS请求并发送CORS头.

// scheme required,here can be multiple origins concatenated by space if using credentials
header('Access-Control-Allow-Origin: http://sub.example.com');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE');
header('Access-Control-Allow-Headers: Accept,X-Requested-With');
// without credentials we can use * for origin
header('Access-Control-Allow-Credentials: true');
header('HTTP/1.1 200 OK',true);

然后我们可以简单地使用crossdomain ajax请求.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...