我需要对外部API执行Ajax调用来获取JSON格式的数据,但我没有得到任何结果。Ajax调用外部API来获取JSON - 使用JSONP
我有这样的控制器:
angular.module('myApp').controller('myCtrl', function($scope, MyService) {
$scope.list = MyService.fetchAll();
});
而这种服务,使Ajax请求到另一个域:
var MyService = function($http) {
this.fetchAll = function() {
console.log('here1');
$http.jsonp('http://other.domain.com/list/?allback=JSON_CALLBACK').success(function(data)
console.log('here2');
return angular.fromJson(data);
}).error(function(data, status, headers, config) {
console.log('here3');
console.log(status);
});
};
};
angular.module('myApp').service('MyService', MyService);
的json返回的就是:
[1,2 ,3,4,5]
当执行代码/请求我没有得到例外的结果,代码中。 success()永远不会被执行。我得到控制台:
here1
here3
404
任何有关如何实施此最佳方法的建议将不胜感激。我是新手。
你确定json会回来吗?你的函数似乎在抛出一个404错误,这意味着它根本没有触及api – theDarse 2014-09-12 19:24:21
是的,请求已经完成,我得到结果[1,2,3,4,5] ...(在网络部分检查,在DevTools) – leticia 2014-09-12 19:40:22