AngularJS - 错误与theMovieDB的API错误、AngularJS、API、theMovieDB

2023-09-13 05:04:15 作者:感情人渣

所以,目前我在一个web应用程序使用的工作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 ..

下面是我的code:

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中的错误:未捕获的SyntaxError:意外的令牌:和状态VAR等于'404'。不过,我可以看到Chrome检查列表...

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

即。

{
   "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 ...

编辑:这似乎与其他服务器一起工作。我改变了网址为 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,你应该提供一个回调。下面是工作code段为您服务。这是相当复杂的,例如的缘故。

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