Reputation: 1419
I'm trying to use the NYT api to work, but I keep getting different errors. I tried using this
$.ajax({
url:"http://api.nytimes.com/svc/news/v3/content/all/all/.json?api-key=xxxxxxxxxxxxxxxxxxxxx:xx:xxxxxxxxxx&callback=?",
dataType: 'jsonp',
success:function(json){
alert("Success");
},
error:function(){
alert("Error");
},
});
When i run this I get
Uncaught SyntaxError: Unexpected token :
I know of the implications making cross domain requests, but i'm not sure if i'm eveing doing this right
Upvotes: 0
Views: 1191
Reputation: 784
I fixed this problem my specifying jsonp in my request url:
http://api.nytimes.com/svc/news/v3/content/all/all/.jsonp?limit=10&api-key={my key}
And then when calling the method only specifying dataType: 'jsonp'
and not jsonp: 'jsonp'
or anything like that.
Upvotes: 0
Reputation: 868
All you're missing is changing .json
to .jsonp
in the url.
Your code is correct, but if you look at the response returned, you'll notice it's not actually a jsonp response; it's just a regular json payload.
Adding the callback parameter doesn't cause the NYT APIs to automatically return jsonp. If you want jsonp, use the .jsonp
format and provide the callback param.
Upvotes: 1
Reputation: 48813
From (RFC 1738, Dec. '94) :
Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL.
So you need either to encode uri manually before making ajax call or pass parameters by 'data'
object:
$.ajax({
url:"http://api.nytimes.com/svc/news/v3/content/all/all/.json",
data: {"api-key":"xxxxxxxxxxxxxxxxxxxxx:xx:xxxxxxxxxx"},
dataType: 'jsonp',
success:function(json){
alert("Success");
},
error:function(){
alert("Error");
},
});
Also I am not specifiying 'callback=?
' parameter , because jquery ajax will send it automatically, if dataType
is jsonp
.
Upvotes: 0