2014-09-25 187 views
0

我想獲得The New York Times的MOVIE REVIEWS API。我嘗試了使用AngularJS,但我沒有提供。可以有人幫助我嗎?我無法獲取JSON數據AngularJS

var app = angular.module('movieApp', []); 
app.controller('movieController', 
    function movieController($scope, $http) { 
     $scope.fetchReviews = function() { 
      var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK'; 
      $http.jsonp(api).success(function(data){ 
       $scope.results = data.results; 
      }); 
     } 

    }); 

HTML

<!DOCTYPE html> 
<html lang="en" ng-app="movieApp"> 
<head> 
<meta charset="UTF-8"> 
<title>Movie Review</title> 
<script type="text/javascript" src="js/angular.min.js"></script> 
<script type="text/javascript" src="js/app.js"></script> 
</head> 
<body> 
<div ng-controller="movieController"> 
    <ul class="review-cards"> 
     <li ng-repeat="item in results"> 
      <h2>{{item.display_title}}</h2> 
      <p>{{item.summary_short}}</p> 
     </li> 
    </ul> 
</div> 

</body> 
</html> 
+2

當fetchReviews函數被觸發 – MohamedAbbas 2014-09-25 04:57:30

+0

對,你需要調用fetchReviews函數。在你發佈的代碼中,它永遠不會被執行。 – Gustav 2014-09-25 05:27:59

回答

0

你必須處理該事件的一些元素或只是把它在你的控制器。

var app = angular.module('movieApp', []); 
app.controller('movieController', 
    function movieController($scope, $http) { 
     // define default value 
     $scope.results = []; 

     $scope.fetchReviews = function() { 
      var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK'; 
      $http.jsonp(api).success(function(data){ 
       $scope.results = data.results; 
      }); 
     } 

     // call that event 
     $scope.fetchReviews(); 
}); 

試試吧。

0
app.controller('movieController', function ($scope, $http) { 
    $scope.fetchReviews = function() { 
     var api = 'http://api.nytimes.com/svc/movies/v2/reviews/all.jsonp?&offset=20&order=by-opening-date&api-key=XXX MY KEY XXX&responce-format=.jsonp&callback=JSON_CALLBACK'; 
     $http.jsonp(api).success(function(data){ 
      $scope.results = data.results; 
     }); 
    } 
    $scope.fetchReviews(); 
}); 

檢查這樣