我需要對外部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