0

现在我只是想让 API 调用正常工作,没什么特别的。最后,我只想要一些基本信息,如名称、运行时间、评级和描述……但那都是以后的事了。我什至无法让 API 调用正常工作。

我已经完成了几个教程,但我似乎遗漏了一些东西。

HTML

<head>
<title>Watch a movie!</title>

<meta charset = "UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="">
<meta name="keywords" content="">

<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.0rc1/angular-route.min.js"></script>
<script src="controllers/movies.js"></script>

</head>
<body>
<div id="wrapper">
<button type="button" class="btn btn-primary btn-lg btn-block">NOW PLAYING</button>
<button type="button" class="btn btn-default btn-lg btn-block">COMING FRIDAY</button>

<!-- PLACEHOLDER -->
<div id="movieInfoBox">
<div ng-controller = "movieController">{{movies}}</div>
</div>

</div> <!-- END WRAPPER -->

JS

var movies = angular.module("movies", []); //quotes are name of this file
movies.controller("movieController", function ($scope, $http){ //quotes are name of function called in index

     $http.jsonp("http://api.rottentomatoes.com/api/public/v1.0/movies/155655062.json?apikey=wq98h8vn4nfnuc3rt2293vru")

     .sucess(function(data)
          {$scope.movies = data;})
     .error(function(data){});
 });
4

1 回答 1

4

您必须在 URL 中包含 JSON_CALLBACK。否则,API 返回 JSON 而不是 JSONP。在下面的代码中,我直接在查询字符串中使用配置对象而不是参数。只是因为它更易于阅读,您也可以使用您的版本并将 &callback=JSON_CALLBACK 添加到 URL。请参阅文档中的“jsonp”

工作小提琴:http: //jsfiddle.net/pascalockert/fM7jb/

控制器中的代码:

$http.jsonp('http://api.rottentomatoes.com/api/public/v1.0/movies/155655062.json', {
    params: {
        apikey: 'wq98h8vn4nfnuc3rt2293vru',
        callback: 'JSON_CALLBACK'
    }
})
.success(function (data) {
    $scope.movies = data;
});
于 2014-02-14T01:47:07.853 回答