AngularJS - TheMovieDB 的 API 出错 [英] AngularJS - Error with theMovieDB's API
问题描述
所以,目前我正在使用 theMovieDB 的 API 开发一个网络应用程序,它是一个 AngularJS 应用程序.我只需要获取所有流行的电影,但它似乎不起作用..
So, currently i'm working on a web-app using theMovieDB's API, and it's an AngularJS application. I just need to get all popular films, but it doesn't seems to work ..
这是我的代码:
var url = 'http://private-anon-7ac1c3824-themoviedb.apiary-mock.com/3/',
key = 'API_KEY'
mode = 'movie/popular';
$http.jsonp(url + mode + key)
.success(function(data) {
alert(data);
}).error(function(data, status) {
console.log(data, status);
});
作为回报,我得到的只是来自 Chrome 的错误:Uncaught SyntaxError: Unexpected token : ,并且状态变量等于404".但是,我可以在 Chrome Inspector 中看到列表...
In return, all I got is an error from Chrome : Uncaught SyntaxError: Unexpected token : , and the status var equals '404'. However, I can see the list in the Chrome Inspector ...
I.E.:
{
"page" : 1,
"results" : {
"adult" : false,
"id" : 82992,
...
}
当我尝试使用普通的$http.get"请求时,它返回一个跨域问题......你们有什么想法吗?我看不到我的错误...
And when I'm trying tu use a normal "$http.get" request, it returns a cross-domain problem ... Do you guys have an idea ? I can't see my mistake ...
它似乎适用于其他服务器.我将 URL 更改为 'http://private-18cc-themoviedb.apiary.io/3/' 并且它现在可以工作了,也许这只是 API 的错误.谢谢各位!
EDIT : It seems to work with an other server. I changed the URL to 'http://private-18cc-themoviedb.apiary.io/3/' and it's working now, maybe it was just an error from the API. Thank you guys !
推荐答案
正确的 MovieDB API(版本 3)端点是 http://api.themoviedb.org/3
.使用 JSONP 时,您应该提供回调.以下是适合您的工作代码片段.例如,它非常详细.
The correct MovieDB API (version 3) endpoint is http://api.themoviedb.org/3
. When using JSONP you should provide a callback. Below is working code snippet for you.
It's quite elaborate for example's sake.
JavaScript
var app = angular.module('plunker', []);
app.controller('MyCtrl', function($scope, $http) {
var base = 'http://api.themoviedb.org/3';
var service = '/movie/popular';
var apiKey = 'just_copy_paste_your_key_here';
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + service + '?api_key=' + apiKey + '&callback=' + callback;
$scope.result = 'requesting...';
$http.jsonp(url).then(function(data, status) {
$scope.result = JSON.stringify(data);
},function(data, status) {
$scope.result = JSON.stringify(data);
});
});
模板
<body ng-controller="MyCtrl">
<h3>MovieDB</h3>
<pre>{{ result }}</pre>
</body>
结果类似于
相关 plunker 在这里http://plnkr.co/edit/gwB60A一个>
Related plunker here http://plnkr.co/edit/gwB60A
这篇关于AngularJS - TheMovieDB 的 API 出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!