admin管理员组

文章数量:1391943

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 = ";apikey=n39jrj4s97tvhxym4qgacnrd&callback=JSON_CALLBACK";
      var url = "/.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 :

Here is the plunker. Plunker

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./v1/sports/news/headlines/top?limit=9&apikey=n39jrj4s97tvhxym4qgacnrd&callback=JSON_CALLBACK";
      var url = "http://api.nytimes./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 :

Here is the plunker. Plunker

Share Improve this question asked Oct 29, 2013 at 21:02 j0hnstewj0hnstew 7098 silver badges25 bronze badges 4
  • 1 That URL isn't JSONP. You can't do that. – SLaks Commented Oct 29, 2013 at 21:02
  • so your saying it does process JSONP. how did you know that? – j0hnstew Commented Oct 29, 2013 at 21:03
  • en.wikipedia/wiki/JSONP You can only use JSONP to send a request to an endpoint that specifically allows JSONP. If you look at the HTTP response with the callback parameter, you must see a function call. – SLaks Commented Oct 29, 2013 at 21:04
  • You could make a proxy on your own server. – SLaks Commented Oct 29, 2013 at 21:09
Add a ment  | 

1 Answer 1

Reset to default 3

Sinse you are calling JSONP, it means that the returned json should be wrappet in a function.

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

Youll need somehow to force the server to support JSONP

If you go to:
http://api.nytimes./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

you can maybe hack it, have a look here:
http://jquery-howto.blogspot.co.il/2013/09/jquery-cross-domain-ajax-request.html

本文标签: javascriptAngularJS Getting Syntax Error in Returned JSON from HTTPJSONPStack Overflow