I'm about to rip out my eyes and eat them. I'm trying to pull data from flickr and apparently I have no idea what I'm doing.
This works:
var flickrAPI = "https://api.flickr.com/services/rest/api_key=xxxxx";
$.get(flickrAPI,
{
method: "flickr.photosets.getList",
format: "json",
user_id: "xxxxx"
}, alert("ok")
);
And I am alerted "ok" However when I try to use function() {} in place of alert()...
var flickrAPI = "https://api.flickr.com/services/rest/api_key=xxxxx";
$.get(flickrAPI,
{
method: "flickr.photosets.getList",
format: "json",
user_id: "xxxxx"
}, function(data) {alert("ok");}
);
Nothing happens.
Also, if I assign the return value of $.get to a variable, I'm left with the following JSON object:
{"readyState" : "1"}
which is not what I'm looking for. any ideas? It might be worth noting that the html file I'm working with is a local file.
Thanks
-
Did you check the request using your developer tools? Probably a same origin issue.TimWolla– TimWolla2014年03月22日 21:55:30 +00:00Commented Mar 22, 2014 at 21:55
-
aha! same origin issue it is. I didn't even know that was a thing. thank you so much for steering me in the right direction.user3450830– user34508302014年03月22日 22:02:13 +00:00Commented Mar 22, 2014 at 22:02
-
I just created an answer containing some more information, you might want to check it :)TimWolla– TimWolla2014年03月22日 22:07:14 +00:00Commented Mar 22, 2014 at 22:07
-
Your alert runs ONLY because it is executed immediately BEFORE the flickrAPI call is even sent. You will need to use JSONP to communicate with flickr.jfriend00– jfriend002014年03月22日 22:17:19 +00:00Commented Mar 22, 2014 at 22:17
2 Answers 2
You cannot retrieve data from another host using AJAX because of the same-origin policy.
It may be possible to use jsonp if the other host supports it. It is explained how to use jsonp with jQuery in the jQuery manual. In the specific case of Flickr there is a blogpost explaining how to use jsonp with flickr.
1 Comment
Since it is API I asume it has Access-Control-Allow-Origin header, so this should work
$.ajax({
url: "https://api.flickr.com/services/rest/api_key=xxxxx",
data: {
method: "flickr.photosets.getList",
format: "json",
user_id: "xxxxx"
},
success: function(data) {alert("ok");},
dataType: "json"
});