Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var getJSON = function(url) {
- return new Promise(function(resolve, reject) {
- var post = new XMLHttpRequest();
- post.open('get', url, true);
- post.responseType = 'json';
- post.onload = function() {
- var status = post.status;
- if (status == 200) {
- resolve(post.response);
- } else {
- reject(status);
- }
- };
- post.send();
- });
- };
- getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=hopollo&api_key=ecb2e8dafb046d7fb3cd906bd866808e&format=json').then(function(data) {
- json_obj = data;
- song_name = json_obj.recenttracks.track[0].name;
- artist_name = json_obj.recenttracks.track[0].artist['#text'];
- //alert("Lecture : " + song_name + ' - ' + artist_name); //you can comment this, i used it to debug
- //console.log(json_obj)
- //result.innerHTML(song_name + artist_name); //display the result in an HTML element
- //JSON.stringify("Lecture : " + song_name + " - " + artist_name);
- //document.getElementById("song").innerHTML = "test";
- document.body.innerHTML = song_name + " - " + artist_name;
- //document.open();
- //document.write(song_name + " - " + artist_name);
- //document.close();
- //console.log(song_name + " - " + "artist_name");
- //var str = document.getElementById("demo").innerHTML;
- //var res = str.replace("Undefined", song_name);
- //document.getElementById("demo").innerHTML = res;
- }, function(status) { //error detection....
- document.body.innerHTML = "unable to retrieve song name.";
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement