I am currently facing an issue while trying to display the cover art along with the search results. There seems to be a problem in the img src tag that is preventing the app from loading properly. Interestingly, when I direct the img to data.tracks[i].album.name (even though it's not a real URL), it works fine. However, as soon as I replace it with an actual URL, the app stops working altogether.
$('#findTracks').click(function (e) {
e.preventDefault(); // prevent form submission
$('#recommendations').empty();
var artist = $('#artist').val();
var userid = "";
var playlistid = "";
$.ajax({
url: 'http://ws.spotify.com/search/1/track.json?q=' + artist,
type: 'GET',
dataType: 'json',
success: function(data) {
if (data.tracks.length > 0) {
var tracksLength = data.tracks.length, html = '';
for (var i=0; i<tracksLength; i++) {
var href = '';
if (data.tracks[i].album.availability.territories.indexOf(' GB ') !== -1) {
href = data.tracks[i].href;
href = 'makeReq(\''+data.tracks[i].name + ' by '+data.tracks[i].artists[0].name+'\')';
html += '<li><a href="#" onclick="' + href + '">' +data.tracks[i].name + ' by '+data.tracks[i].artists[0].name+ ' <img src="' +data.tracks[i].album.images[0].url+ '" /></a>';html += '</li>';
html += '</li>';
}
}
$('#third').css('display', 'block');
$('#recommendations').append(html);
} else {
$('#recommendations').append('<li>No matches returned.</li>');
$('#third').css('display', 'none');
}
},
error: function(err) {
alert("The Spotify API failed to return a response.");
}
});
});
This is my first attempt at coding in JavaScript, so please bear with me!
EDIT:
It seems like the code is functioning properly now! However, some of the songs don't respond when I click on them
For instance, when I search for "Don't Stop," only "The Black Eyed Peas - Don’t Stop The Party" seems to work out of the initial ten results...Does anyone know why?
Also, why is "if (data.tracks[i].album.availability.territories.indexOf(' GB ') !== -1)" included in the code? Removing it causes everything to stop working...I am not located in G.B.