Last active
August 29, 2015 14:06
-
-
Save nekman/60656d9e59ddb6d64ffc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toYQL(url) { | |
var yqlUrl = 'http://query.yahooapis.com/v1/public/yql?q=', | |
query = 'select * from json where url="{url}"'.replace('{url}', url); | |
return yqlUrl + encodeURIComponent(query) + '&format=json'; | |
} | |
function getCORSRequest(method, url) { | |
return new Promise(function(resolve, reject) { | |
var xhr = typeof XDomainRequest !== 'undefined' ? new XDomainRequest() : new XMLHttpRequest(); | |
xhr.open(method, url, true); | |
xhr.onload = function() { | |
resolve(JSON.parse(xhr.responseText)); | |
}; | |
xhr.onerror = reject; | |
xhr.send(); | |
}); | |
} | |
var url = 'http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1'; | |
getCORSRequest('GET', toYQL(url)).then(function(data) { | |
success(data.query.results.json); | |
}).catch(function(err) { | |
console.error(err); | |
}) | |
function success (data) { | |
var results = document.querySelector('#results'); | |
data.items.forEach(function(item) { | |
var el = document.createElement('div'); | |
el.innerHTML = item.description; | |
results.appendChild(el); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment