我一直收到这个错误" angular.js:TypeError:无法读取属性'页面'未定义"

时间:2017-11-24 06:08:41

标签: javascript jquery angularjs

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

这是我的角度脚本。我还使用$scedelegateProvider将URL设置为白名单。它用于调用Wikipedia API。请帮我解决此问题。

1 个答案:

答案 0 :(得分:1)

我已经按照以下方式进行了测试,结果很好。

<强> 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

enter image description here

试试这个。 :)