以下电话在AngularJS中为我工作:
var baseUrl = 'http://aaa.com/';
return $http.jsonp(baseUrl + 'root/' + path + '?action=doit&callback=JSON_CALLBACK';
现在,我已决定尝试使用Restangular并将其替换为以下内容:
return Restangular.all('root/' + path).getList({action: 'doit', callback: 'JSON_CALLBACK'} );
但我收到错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9000' is therefore not allowed access.
我做错了什么?
我按如下方式初始化Restangular:
.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://aaa.com/');
RestangularProvider.setDefaultRequestParams('jsonp', {callback: 'JSON_CALLBACK'});
});
答案 0 :(得分:0)
要启用CORS,您必须进行以下更改。别忘了服务器端也需要支持CORS。
在app.js中注入httpProvider并执行以下操作。
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];