jsonFlickrFeed({
"title": "Uploads from everyone",
"link": "http://www.flickr.com/photos/",
"description": "",
"modified": "2009-12-07T23:23:50Z",
"generator": "http://www.flickr.com/",
"items": [
{
"title": "IMG_0334",
"link": "http://www.flickr.com/photos/ccie/4166986893/",
"media": {"m":"http://farm3.static.flickr.com/2604/4166986893_db94b48b0e_m.jpg"},
"date_taken": "2009-12-02T15:55:07-08:00",
"description": "
Typically JSON requests reply with an object (just {...}), which, if loaded in a script tag, wouldn't do anything. Flickr actually wraps it's response in a method called "jsonFlickrFeed" which it expects you to define. But not all services do this by default.
What we can do is add to the url "jsoncallback=blah({...json info...})
So now we can include that script tag in our body and it will execute our function ('blah') with the json data returned. Nifty, eh?
Enter //Request.JSONP//, a class that works a lot like the Ajax class, but does this json stuff instead. Here's a simple example:
new Request.JSONP({
callbackKey: 'jsoncallback',
url: 'http://www.flickr.com/services/feeds/photos_public.gne?format=json',
onComplete: function(data){
new Element('div').adopt(
new Element('img', {
src: data.items[0].media.m
})
).inject($('flickr1'));
}
}).send(); //injects the first image into the dom element #flickr1
Again, works pretty much like Ajax does. In order to work, you have to specify the callback string. It defaults to "callback" so I have to specify it here because Flickr expects 'jsoncallback'. You can also specify a query string for the url and additional data as an object. Here's the same thing as above, but using these two other options:
new Request.JSONP('http://www.flickr.com/services/feeds/photos_public.gne', {
callbackKey: 'jsoncallback',
data: {
tags: 'blue'
},
queryString: 'format=json',
onComplete: function(data){
new Element('div').adopt(
new Element('img', {
src: data.items[0].media.m
})
).inject($('flickr2'));
}
}).send(); //gets the first image with the tag "blue"