简体   繁体   中英

I keep getting this error “angular.js:TypeError: Cannot read property 'pages' of undefined”

The error in console after console.log(data)

var app = angular.module("myApp", []).config(function($sceDelegateProvider) {
  $sceDelegateProvider.resourceUrlWhitelist(['**']);
});

app.controller("myCtrl",["$scope","$window", "$http",
    function($scope, $window, $http,) {

      //variable to get input
      var input = $('input');
      var search = $("#search");
      var form = $('form');

      //Function for random wiki article
      $scope.Random = function() {
          $window.open("https://en.wikipedia.org/wiki/Special:Random", "_blank");
    };


     //function for searching wiki article 
     $scope.search = function() {
       $scope.result = [];
       var title = input.val();
         var api = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=";
         //var cb = "&callback=JSON_CALLBACK";
         var page = "https://en.wikipedia.org/?curid=";
       $http.jsonp(api + title, {jsonpCallbackParam: 'callback'}).then(function(data) { var results = data.query.pages;
    console.log(data);            
      angular.forEach(results, function(v,k)  {
        $scope.results.push({title: v.title, body: v.extract, page: page + v.pageid})
      })
    });
   };
    }]);  

This is my angular script.I have also used $scedelegateProvider to make the URL in white-list. It is used for calling the Wikipedia API.Help me please to resolve this issue.

I've tested like below and my result was fine.

CODE

var title = 'Angularjs';
var api = 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=';

var url = $sce.trustAsResourceUrl(api+title);

$http.jsonp(url, {
    headers: {
        'Access-Control-Allow-Origin' : '*',     // add this
        'Content-Type': 'application/json'
    }
}).then(function(data) {
    console.log(data.data.query.pages);
});

CONSOLE

在此处输入图片说明

Try this. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM