我无法获取JSON数据AngularJS

时间:2014-09-25 04:48:27

标签: json angularjs api

我想获得纽约时报的电影评论API。我尝试使用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 个答案:

答案 0 :(得分: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();
});

像这样检查

答案 1 :(得分: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();
});

试试吧。