Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Michael Williamson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michael Williamson
Front End Web Development Techdegree Graduate 23,903 Points

Spotify API

Learning to work with API's here, trying to loop through this JSON and get the url for images for starters, once I get that going I feel I can get other bits of data, this is as far as I've gotten but don't understand whats not working and how to make it work. Thanks.

$("button").click(function(){
  var spotify = "https://api.spotify.com/v1/artists/4EVpmkEwrLYEg6jIsiPMIb/top-tracks?country=US";
  function displayPhotos(data){
    var html = '<ul>';
    $.each(data.tracks, function(i,eachTrack){
      html += '<li>';
      html += '<img src"' + eachTrack.images[0].url + '">';
      html += '</li>';
    });
    html += '</ul>';
    $("main").html(html);
  }
  $.getJSON(spotify,displayPhotos);
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link type="text/css" rel="stylesheet" href="css/styles.css">
  <title>Project 10</title>
</head>
<body>
  <header>
    <button type="button">click for view</button>
  </header>
  <main>

  </main>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="/js/script.js"></script>
</body>
</html>

2 Answers

Michael Liendo
Michael Liendo
15,326 Points

Not much to change :) Just note that each track is contained in an "album" object, so I added that, and secondly, I just added an equal sign to your img src attribute.

$("button").click(function(){
  var spotify = "https://api.spotify.com/v1/artists/4EVpmkEwrLYEg6jIsiPMIb/top-tracks?country=US";
  function displayPhotos(data){
    var html = '<ul>';
    $.each(data.tracks, function(i,eachTrack){
      html += '<li>';
      html += '<img src="' + eachTrack.album.images[0].url + '">';
      html += '</li>';
    });
    html += '</ul>';
    $("main").html(html);
  }
  $.getJSON(spotify,displayPhotos);
});

Working codepen for those interested: http://codepen.io/qctimes/pen/ENKvYw?editors=1011