简体   繁体   English

AngularJS从HTTP.JSONP返回JSON中获取语法错误

[英]AngularJS Getting Syntax Error in Returned JSON from HTTP.JSONP

I am having trouble getting the NYTimes API to return the JSON correctly. 我无法让NYTimes API正确返回JSON。 I am sure it is something I am doing wrong versus them. 我确信这是对我们做错的事情。 I tried this using the ESPN API and it worked fine. 我尝试使用ESPN API,它工作正常。 Not sure what I am missing. 不确定我错过了什么。 Here is a look at the code. 这是一个代码。

app.controller('espn', function($scope,$http){
      //var url = "http://api.espn.com/v1/sports/news/headlines/top?limit=9&apikey=n39jrj4s97tvhxym4qgacnrd&callback=JSON_CALLBACK";
      var url = "http://api.nytimes.com/svc/news/v3/content/all/all/.json?&limit=20&api-key=1f6ef65ff5bb290bdcb01da786c788de:2:67858849&callback=JSON_CALLBACK";
     $http.jsonp(url)
        .success( function(data){
            console.log(data);
      });
});

I get this error in my error console. 我在错误控制台中收到此错误。 Uncaught SyntaxError: Unexpected token : 未捕获的SyntaxError:意外的令牌:

Here is the plunker. 这是掠夺者。 Plunker Plunker

Sinse you are calling JSONP, it means that the returned json should be wrappet in a function. 你正在调用JSONP,这意味着返回的json应该是函数中的包装。

for example: 例如:

JSON_CALLBACK({"status":"OK"});//this is actually how the server suppose to answer back

So, I see that you send callback=JSON_CALLBACK , but the server does not reply with function call to JSON_CALLBACK 所以,我看到你发送了callback=JSON_CALLBACK ,但服务器没有回复函数调用JSON_CALLBACK

Youll need somehow to force the server to support JSONP 你需要以某种方式强制服务器支持JSONP

If you go to: 如果你去:
http://api.nytimes.com/svc/news/v3/content/all/all/.json?&limit=20&api-key=1f6ef65ff5bb290bdcb01da786c788de:2:67858849&callback=JSON_CALLBACK http://api.nytimes.com/svc/news/v3/content/all/all/.json?&limit=20&api-key=1f6ef65ff5bb290bdcb01da786c788de:2:67858849&callback=JSON_CALLBACK
youll see that the server is not responding as JSONP 你会看到服务器没有响应为JSONP

you can maybe hack it, have a look here: 你可以破解它,看看这里:
http://jquery-howto.blogspot.co.il/2013/09/jquery-cross-domain-ajax-request.html http://jquery-howto.blogspot.co.il/2013/09/jquery-cross-domain-ajax-request.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM