5

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

4
  • 1
    That URL isn't JSONP. You can't do that. Commented Oct 29, 2013 at 21:02
  • so your saying it does process JSONP. how did you know that? Commented Oct 29, 2013 at 21:03
  • en.wikipedia.org/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. Commented Oct 29, 2013 at 21:04
  • You could make a proxy on your own server. Commented Oct 29, 2013 at 21:09

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

I mean it is up to the server to do such a thing if they want to correct? I don't think I can force the server to do such a thing.
you cant, if the API documentation says so, so you will be able to. Sometime api are blocking that in order to prevent robots scanning the ajax servers, but if I look at the server response, you can make your server call their server and than you will return the message to the javascript.
I see that now. So I guess if it doesn't support it, then there is no way of forcing it to, meaning that I will have to use PHP or Python to CURL it then make a request to that file and get the JSON from there...Correct?
I edit my answer, you can create your own server calling their server. see the link i published.
I think I'll just write a little server side code to handle this then I'll be good to go. Thanks for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.