Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Getting Syntax Error in Returned JSON from HTTP.JSONP

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);
      });
});

I get this error in my error console. Uncaught SyntaxError: Unexpected token :

Here is the plunker. Plunker

like image 937
j0hnstew Avatar asked Oct 21 '22 21:10

j0hnstew


1 Answers

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

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

like image 50
Avi Fatal Avatar answered Oct 27 '22 11:10

Avi Fatal