Angular:访问json对象时出错

时间:2017-02-16 16:00:15

标签: angularjs json ajax http

在我的控制器里面我有一个$ http.get()函数,在$ http里面我有以下几行代码 -

248            console.log($scope.ticketAPIData);
249
250            var j = 0;
251            while(j < response.data.d.results.length){
252              console.log($scope.ticketAPIData[j].data.Login_ID);
253              j++;
254            }

第248行的第一个console.log产生的数据我可以在控制台中展开,但如果我在while循环中使用相同的范围变量并尝试访问单个数据元素(第252行),那么我会收到错误。

Chrome控制台错误 -

Array[0]
    0: Object
        config: Object
        data: Object
            First_Name: "Rahul"
            Full_Name: "Rahul A Parelkar"
            Login_ID: "**********"
            __proto__: Object
        headers: (d)arguments: (...)caller: (...)length: 1name: ""prototype: Object__proto__: ()[[FunctionLocation]]: angular.js:9518[[Scopes]]: Scopes[3]
        status: 200
        statusText: "OK"__proto__: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    9: Object
    10: Object
    11: Object
    12: Object
    13: Object
    14: Object
    15: Object
    16: Object
    17: Object
    18: Object
    length: 19
    __proto__: Array[0]

angular.js:12520 TypeError: Cannot read property 'data' of undefined
    at app.js:252
    at angular.js:14792
    at r.$eval (angular.js:16052)
    at r.$digest (angular.js:15870)
    at r.$apply (angular.js:16160)
    at g (angular.js:10589)
    at T (angular.js:10787)
    at XMLHttpRequest.w.onload (angular.js:10728)

完成$ http -

 $http.get('https://path/ListData.svc/TeamList').
  then(function(res) {
      //shifting the response from rest call(res) into local variable(response)
      var response = res;
      $scope.evolveTeam = [];
  var i = 0;
  var lanID = [];
  var domain = [];
  var displayPicture = [];
  var jobTitle = [];
  var searchName = [];
  var apiURL = [];
  var userData = [];
  var imageURL = [];
  var imageData = [];
  $scope.ticketAPIData = [];

  while (i < response.data.d.results.length) {

      searchName.push(response.data.d.results[i].Domain + response.data.d.results[i].LanID);
      lanID.push(response.data.d.results[i].LanID);
      domain.push(response.data.d.results[i].Domain);
      jobTitle.push(response.data.d.results[i].Job_title);
      displayPicture.push(response.data.d.results[i].Display_picture);
      //var deferred = $q.defer();
      apiURL.push('https://path/api/user/');
      imageURL.push('https://path/Profile%20Pictures/');
      userData.push(response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID);
      imageData.push(response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID + '_LThumb.jpg'); 
      response.data.d.results[i].DomainLanID = (response.data.d.results[i].Domain + '\\' + response.data.d.results[i].LanID).toLowerCase();
      //SP list returns the image URL with caption in the same string so removing
      //everything after the http://../../abc.jpg
      if (response.data.d.results[i].Display_picture) {
          //console.log(displayPicture[j]);
          var s = response.data.d.results[i].Display_picture.toString();
          var n = s.indexOf(',');
          s = s.substring(0, n != -1 ? n : s.length);
          response.data.d.results[i].Display_picture = s;
      }


      $http.get('https://path/api/user/' + response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID, {
          withCredentials: true
      }).then(
          function(successResponse) {

              $scope.ticketAPIData.push(successResponse);
          },
          function(failedResponse) {});
      */

      i++;
  }




  console.log(response.data.d.results);

  console.log($scope.ticketAPIData);

  var j = 0;
  while (j < response.data.d.results.length) {
      console.log($scope.ticketAPIData[j].data.Login_ID);
      j++;
  }




 });

2 个答案:

答案 0 :(得分:0)

如果我假设您正确,请将while循环更改为

while(j < response[j].data.d.results.length){
          console.log($scope.ticketAPIData[j].data.Login_ID);
        j++;
  }

希望它能奏效。

答案 1 :(得分:0)

我的理解:

根据252 中行号app.js的控制台错误:

  

TypeError:无法读取属性&#39;数据&#39;未定义的

您正在访问data对象$scope.ticketAPIData[j]的{​​{1}}属性。在这里,undefinedj = 0

观察:

  • 如果< response.data.d.results.length大于response.data.d.results.length数组的长度,则会发生此错误。

<强>样本

&#13;
&#13;
$scope.ticketAPIData
&#13;
&#13;
&#13;