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 trialMichael Williamson
Front End Web Development Techdegree Graduate 23,903 PointsSpotify 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
15,326 PointsNot 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
Tracy Excell
Front End Web Development Techdegree Graduate 15,333 Points"https://api.spotify.com/v1/artists/4EVpmkEwrLYEg6jIsiPMIb/top-tracks?country=US"
Hi Michael Williamson, how do you find this link to the artists albums?
Michael Williamson
Front End Web Development Techdegree Graduate 23,903 PointsMichael Williamson
Front End Web Development Techdegree Graduate 23,903 Pointsthat was it, new with JSON so its challenging to read but I see now the path that it took to arrive at that property. thank you for the help.