在 Angular JS 1.6 中处理 JSONP 响应



如何处理 JSON 响应? 我试图搜索,但我无法解决它。 下面的屏幕截图显示了 JSON 结果。 我使用此代码获得 JSONP 响应,服务.js

var app=angular.module('F1FeederApp.services', []);
app.config(function($sceDelegateProvider) {  
	$sceDelegateProvider.resourceUrlWhitelist([
'self',
'http://ergast.com/**'
]);
});
app.factory('ergastAPIservice', function($http) {
var ergastAPI = {};
var urlFiltered = 'http://ergast.com/api/f1/current/driverStandings.json';
ergastAPI.getDrivers = function() {
return $http({
method: 'JSONP', 
url: urlFiltered
});
}

return ergastAPI;
});

现在,我使用下面的代码访问它,并在第一张图片上给我结果。

angular.module('F1FeederApp.controllers', []).
controller('driversController', function($scope, ergastAPIservice) {
$scope.nameFilter = null;
$scope.driversList = [];
// //ergastAPIservice.getDrivers() ->> when i try this i get error this is not a function.
//ergastAPIservice.getDrivers().success(function (response) {
//Dig into the responde to get the relevant data
//    $scope.driversList = response.MRData.StandingsTable.StandingsLists[0].DriverStandings;
//});
//code above doesnt work so i tried to access it or atleast show a value like
// the code below
console.log(ergastAPIservice.getDrivers());
console.log(ergastAPIservice.getDrivers().MRData.StandingsTable.StandingsLists[0].DriverStandings);
});

现在我使用 console.log(JSONP 响应(获得第 1 张图片。 如何获取该响应中的驱动程序列表? like: collectionVar = response.getDrivers((;. 任何链接或相同的问题链接都会有所帮助,谢谢!

做这样的事情。这应该有效.你得到了一个承诺.承诺的处理方式如下。了解有关承诺的更多信息

app.controller("testController", function($scope,testService){
testService.getDrivers ()
.then(function (response) {
$scope.standingTable = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings;
// handle valid reponse
},
function(error) {
//handel error
});

}

非常感谢诺曼! 起初我不知道我得到了一个角度的承诺回应。 虽然我想得到司机站立名单。 所以我使用这个代码。

$scope.driversList = response.data.MRData.StandingsTable.StandingsLists[0].DriverStandings;

最新更新