在角JS 1.6中处理JSONP响应。

时间:2021-09-06 12:11:50

how can i handle a jsonp response? i tried to search but i cant solve it. the screen shot below shows a jsonp result.1 i get that jsonp response using this code, services.js

如何处理jsonp响应?我试着去寻找,但我无法解决。下面的屏幕截图显示了jsonp结果。我使用这个代码services.js获得jsonp响应

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;
  });

now, i access it using the code below and gives me result on the 1st picture.

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

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);
  });

now i get the 1st picture using console.log(jsonp response). how can i get the list of drivers in that response?, like: collectionVar = response.getDrivers();. any link or same problem links would help thanks!

现在我用控制台得到第一张图片。日志(jsonp响应)。如何得到响应中的驱动程序列表?,如:collectionVar = response.getDrivers();。任何链接或相同的问题链接将会帮助谢谢!

2 个解决方案

#1


1  

Do something like this. This should work . You are getting a promise . Promises are handled like below. Learn more about promise

这样做。这应该工作。你得到了一个承诺。承诺的处理方式如下。了解更多关于承诺

   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
            });


    }

#2


0  

Big thanks for Noman! i didnt know that i was getting a angular promise response at first. though i want to get the driver standing list. so i use this code.

大感谢诺曼!一开始我不知道我得到了一个有棱角的承诺。虽然我想要司机名单。所以我用这个代码。

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

#1


1  

Do something like this. This should work . You are getting a promise . Promises are handled like below. Learn more about promise

这样做。这应该工作。你得到了一个承诺。承诺的处理方式如下。了解更多关于承诺

   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
            });


    }

#2


0  

Big thanks for Noman! i didnt know that i was getting a angular promise response at first. though i want to get the driver standing list. so i use this code.

大感谢诺曼!一开始我不知道我得到了一个有棱角的承诺。虽然我想要司机名单。所以我用这个代码。

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