Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. var getJSON = function(url) {
  2.  
  3. return new Promise(function(resolve, reject) {
  4.  
  5. var post = new XMLHttpRequest();
  6.  
  7. post.open('get', url, true);
  8.  
  9. post.responseType = 'json';
  10.  
  11. post.onload = function() {
  12.  
  13. var status = post.status;
  14.  
  15. if (status == 200) {
  16.  
  17. resolve(post.response);
  18.  
  19. } else {
  20.  
  21. reject(status);
  22.  
  23. }
  24.  
  25. };
  26.  
  27. post.send();
  28.  
  29. });
  30.  
  31. };
  32.  
  33. getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=hopollo&api_key=ecb2e8dafb046d7fb3cd906bd866808e&format=json').then(function(data) {
  34.  
  35. json_obj = data;
  36.  
  37. song_name = json_obj.recenttracks.track[0].name;
  38.  
  39. artist_name = json_obj.recenttracks.track[0].artist['#text'];
  40.  
  41. //alert("Lecture : " + song_name + ' - ' + artist_name); //you can comment this, i used it to debug
  42.  
  43. //console.log(json_obj)
  44.  
  45. //result.innerHTML(song_name + artist_name); //display the result in an HTML element
  46.  
  47. //JSON.stringify("Lecture : " + song_name + " - " + artist_name);
  48.  
  49. //document.getElementById("song").innerHTML = "test";
  50.  
  51. document.body.innerHTML = song_name + " - " + artist_name;
  52.  
  53. //document.open();
  54. //document.write(song_name + " - " + artist_name);
  55. //document.close();
  56.  
  57. //console.log(song_name + " - " + "artist_name");
  58. //var str = document.getElementById("demo").innerHTML;
  59. //var res = str.replace("Undefined", song_name);
  60. //document.getElementById("demo").innerHTML = res;
  61.  
  62. }, function(status) { //error detection....
  63.  
  64. document.body.innerHTML = "unable to retrieve song name.";
  65.  
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement