【发布时间】:2015-04-12 00:55:47
【问题描述】:
我已经创建了这个 ListFactory
factory('ListFactory', ['$resource', function ($resource) {
// var Chiamate = $resource('http://samedomain.com/file.json', {}, {
var Chiamate = $resource('http://anotherdomain.com/file.json', {}, {
query: {
// method: 'JSONP',
method: 'GET',
isArray: true,
cache : false,
}
});
return {
query: function(id) {
chiamate = Chiamate.query();
return chiamate;
}
};
}]);
在控制器中我调用工厂使用 $scope.results = ListFactory.query();
并在 html 中显示结果 {{结果}}
如果我尝试在同一域中下载 json 文件 http://samedomain.com/file.json 使用方法:'GET',=> 好的,我看到了资源
如果我尝试在另一个域中下载 json 文件 http://anotherdomain.com/file.json 使用方法:'GET',=> KO 我看到这个错误:
XMLHttpRequest 无法加载 http://anotherdomain.com/file.json 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://anotherdomain.com/file.json' 因此不允许访问。
如果我尝试在另一个域中下载 json 文件 http://anotherdomain.com/file.json 使用方法:'JSONP',=> KO 我没有看到任何错误,但我没有看到结果:
我用https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js
有什么想法吗?你能帮我在另一个域上获取 json 文件吗?
【问题讨论】:
-
您是否有一些服务器端代码将您的 json 转换为 jsonp?如果没有,您唯一的选择是使用其他技术来执行此操作,例如服务器端 php/asp/node 等,或者在 anotherdomain.com 上实现 CORS。