JSON response returned as file in ajax call
I am calling the REST API service of Vimeo using the following code snippet, the data comes out as null with a 200 OK call.
$.ajax({
url:'http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.getInfo&video_id=7100569',
dataType:'json',
success:function(data){
alert(data);
}
开发者_JAVA技巧 });
When I copy paste the url (http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.getInfo&video_id=7100569)
in the browser it gives me a file to download.
I checked out the sample javascript examples which do work
var url='http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.getInfo&video_id=7100569';
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', url);
document.getElementsByTagName('head').item(0).appendChild(js);
I just wanted to know what am I doing wrong here. Note the second snippet gives a valid JSON error instead of null response.
After look at it several times (and posting then deleting my comment), this is what I came up with:
The fact that you are getting a "file" is just a function of how the browser is treating the result. Chrome displayed the result just fine while IE tried to save it off to a file.
You are having an issue because you can't do AJAX requests to a domain that is different than the one the page originated from. This is what David Dorward was getting at. The example they provide is jsonp (which is different than json). Essentially, you should be able to change the dataType in your ajax call to 'jsonp', and hopefully it will start working for you.
HTH
The data is JSON, not JSON-P. It isn't JSON-P so you can't use it as JavaScript.
Try using the method described here.
精彩评论