无法从请求中获取数据

时间:2015-12-03 21:29:23

标签: angularjs json

我是json的新手,无法从请求中获取正确的数据。 我getDrivers()没有遇到任何问题,但当我尝试从getDriverRaces($scope.id)获取数据时,我什么都没得到。 我已经尝试过这样做:http://ergast.com/api/f1/2015/drivers/hamilton/results.json?callback=JSON_CALLBACK 结果是 - enter image description here

      angular.module('F1FeederApp.controllers', []).

  controller('driversController', function ($scope, ergastAPIservice) {
      $scope.nameFilter = null;
      $scope.driversList = [];
      $scope.searchFilter = function (driver) {
          var re = new RegExp($scope.nameFilter, 'i');
          return !$scope.nameFilter || re.test(driver.Driver.givenName) || re.test(driver.Driver.familyName);
      };

      ergastAPIservice.getDrivers().success(function (response) {
          $scope.driversList = response.MRData.StandingsTable.StandingsLists[0].DriverStandings;
      });
  }).

  controller('driverController', function ($scope, $routeParams, ergastAPIservice) {
      $scope.id = $routeParams.id;
      $scope.races = [];
      $scope.driver = null;

      ergastAPIservice.getDriverRaces($scope.id).success(function (response) {
          $scope.races = response.MRData.RaceTable.Races;
      });
  });
 <div class="main-content">
        <table class="result-table">
            <thead>
                <tr><th colspan="5">Formula 1 2015 Results</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td>Round</td>
                    <td>Grand Prix</td>
                    <td>Team</td>
                    <td>Grid</td>
                    <td>Race</td>
                </tr>
                <tr ng-repeat="race in races">
                    <td>{{race.round}}</td>
                    <td>{{race.raceName}}</td>
                    <td>{{race.Results[0].Constructor.name}}</td>
                    <td>{{race.Results[0].grid}}</td>
                    <td>{{race.Results[0].position}}</td>
                </tr>
            </tbody>
        </table>
    </div>
angular.module('F1FeederApp.services', [])
  .factory('ergastAPIservice', function ($http) {

      var ergastAPI = {};

      ergastAPI.getDrivers = function () {
          return $http({
              method: 'JSONP',
              url: 'http://ergast.com/api/f1/2015/driverStandings.json?callback=JSON_CALLBACK'
          });
      }

      ergastAPI.getDriverRaces = function (id) {
          return $http({
              method: 'JSONP',
              url: 'http://ergast.com/api/f1/2015/drivers/' + id + '/results.json?callback=JSON_CALLBACK'
          });
      }

      return ergastAPI;
  });

任何帮助都会表示赞赏。

2 个答案:

答案 0 :(得分:1)

问题是由于粗心大意,我输入了两次相同的控制器,因为它的名字很常见。

    android {
     ......

    productFlavors {
        branch1 {
            buildConfigField("String", "WHICH_BRANCH", "Branch1")
        }
        branch2 {
            buildConfigField("String", "WHICH_BRANCH", "Branch2")
        }
    }
    .........
}

答案 1 :(得分:0)

我认为您的问题是如何尝试获取成功函数中的数据。

这个

ergastAPIservice.getDriverRaces($scope.id).success(function (response) {
    $scope.races = response.MRData.RaceTable.Races;
});

应该是这样的($ http调用返回的数据在response.data中)

ergastAPIservice.getDriverRaces($scope.id).success(function (response) {
    $scope.races = response.data.MRData.RaceTable.Races;
}); 

响应对象具有以下属性(see documentation here near top

  • 数据 - 您的回复应该在这里
  • 状态
  • 标题
  • 配置
  • 状态文本

您正在尝试访问属性response.MRData,该属性永远不会存在,但response.data.MRData应该