AngularJS从HTTP.JSONP返回JSON中获取语法错误 [英] AngularJS Getting Syntax Error in Returned JSON from HTTP.JSONP
问题描述
我无法让NYTimes API正确返回JSON。我确信这是对我们做错的事情。我尝试使用ESPN API,它工作正常。不确定我错过了什么。下面是代码。
I am having trouble getting the NYTimes API to return the JSON correctly. I am sure it is something I am doing wrong versus them. I tried this using the ESPN API and it worked fine. 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);
});
});
我的错误控制台出现此错误。
未捕获的SyntaxError:意外的令牌:
I get this error in my error console. Uncaught SyntaxError: Unexpected token :
以下是plunker。 Plunker
Here is the plunker. Plunker
推荐答案
Sinse you正在调用JSONP,这意味着返回的json应该是函数中的包装。
Sinse you are calling JSONP, it means that the returned json should be wrappet in a function.
例如:
JSON_CALLBACK({"status":"OK"});//this is actually how the server suppose to answer back
所以,我看到你发送 callback = JSON_CALLBACK
,但服务器没有回复函数调用 JSON_CALLBACK
So, I see that you send callback=JSON_CALLBACK
, but the server does not reply with function call to JSON_CALLBACK
您需要以某种方式强制服务器支持 JSONP
Youll need somehow to force the server to support JSONP
如果你去:
http: //api.nytimes.com/svc/news/v3/content/all/all/.json?&limit=20&api-key=1f6ef65ff5bb290bdcb01da786c78 8de:2:67858849& callback = JSON_CALLBACK
你会看到服务器没有响应为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
youll see that the server is not responding as JSONP
你可能会破解它,看看这里:
http://jquery-howto.blogspot.co.il/2013/09/jquery-cross-domain-ajax-request.html
you can maybe hack it, have a look here:
http://jquery-howto.blogspot.co.il/2013/09/jquery-cross-domain-ajax-request.html
这篇关于AngularJS从HTTP.JSONP返回JSON中获取语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!