0

I have the below issue, I believe I have coded the site to do two consecutive API calls, but am unable to get any data from the second call into my div.

$(document).ready(function() {
    $(".discogs-loading").show();
    $.getJSON("http://api.discogs.com/users/bengeorgebrooks/collection/folders/0/releases?callback=&sort=added&sort_order=desc&per_page=1000", function(data) {
        $(".discogs-loading").hide();
        if (data.pagination.items > 1) {
            $.each(data.releases, function(i, release) {
            $.getJSON("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=cf59c4a923ea2f7743bd8899ec439581&artist=" + release.basic_information.artists[0].name + "&album=" + release.basic_information.title + "&format=json", function(data) {                    
            });
                content =
                    '<li class="folder"><div class="img"></div><a class="name" href="#">' + release.basic_information.artists[0].name + '</a><p class="type">' + release.basic_information.title + '</p><ul class="video_tags"><li class="tag1">' + release.basic_information.labels[0].name + ' ·</li><li class="tag2">' + release.basic_information.formats[0].descriptions[0] + ' ·</li><li class="tag3">' + release.basic_information.year + '</li></ul><ul class="video_tags"><li class="tag4">' + release.basic_information.genres[0] +  ' ·</li><li class="tag5">' + release.basic_information.styles[0] +  ' ·</li><li class="tag5">' + + '</li></ul><ul class="video_tags"><li class="tag6">10 Plays ·</li><li class="tag7"> 20th May 2021</li></ul><p class="pinned icon-pinned"><img src="last_fm.png" height="25px">&nbsp;&nbsp;&nbsp;<img src="heart.png" height="15px"></p></li>';
                $(content).appendTo("#discogs-collection");
            });
            $(".discogs-link").click(function() {
                window.open($(this).data("href"));
            });
        } else {
            $("#discogs-collection").append("<tr class='discogs-error'><td>Error! Something is up with the Discogs API.</td></tr>");
        }
    });
});

My expectation is that I should be able to data.album.name to get the album name from Last.FM, but this is not working and either returns undefined or just prevents the page loading.

Example Last.FM API result

{"album":{"name":"Believe","artist":"Cher","mbid":"63b3a8ca-26f2-4e2b-b867-647a6ec2bebd","url":"https://www.last.fm/music/Cher/Believe","image":[{"#text":"https://lastfm.freetls.fastly.net/i/u/34s/3b54885952161aaea4ce2965b2db1638.png","size":"small"},{"#text":"https://lastfm.freetls.fastly.net/i/u/64s/3b54885952161aaea4ce2965b2db1638.png","size":"medium"},{"#text":"https://lastfm.freetls.fastly.net/i/u/174s/3b54885952161aaea4ce2965b2db1638.png","size":"large"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":"extralarge"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":"mega"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/3b54885952161aaea4ce2965b2db1638.png","size":""}],"listeners":"477539","playcount":"3311984","tracks":{"track":[{"name":"Believe","url":"https://www.last.fm/music/Cher/_/Believe","duration":"240","@attr":{"rank":"1"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"The Power","url":"https://www.last.fm/music/Cher/_/The+Power","duration":"236","@attr":{"rank":"2"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Runaway","url":"https://www.last.fm/music/Cher/_/Runaway","duration":"286","@attr":{"rank":"3"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"All or Nothing","url":"https://www.last.fm/music/Cher/_/All+or+Nothing","duration":"237","@attr":{"rank":"4"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Strong Enough","url":"https://www.last.fm/music/Cher/_/Strong+Enough","duration":"223","@attr":{"rank":"5"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Dov'e L'amore","url":"https://www.last.fm/music/Cher/_/Dov%27e+L%27amore","duration":"258","@attr":{"rank":"6"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Takin' Back My Heart","url":"https://www.last.fm/music/Cher/_/Takin%27+Back+My+Heart","duration":"272","@attr":{"rank":"7"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Taxi Taxi","url":"https://www.last.fm/music/Cher/_/Taxi+Taxi","duration":"304","@attr":{"rank":"8"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"Love Is the Groove","url":"https://www.last.fm/music/Cher/_/Love+Is+the+Groove","duration":"271","@attr":{"rank":"9"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}},{"name":"We All Sleep Alone","url":"https://www.last.fm/music/Cher/_/We+All+Sleep+Alone","duration":"310","@attr":{"rank":"10"},"streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"https://www.last.fm/music/Cher"}}]},"tags":{"tag":[{"name":"pop","url":"https://www.last.fm/tag/pop"},{"name":"90s","url":"https://www.last.fm/tag/90s"},{"name":"dance","url":"https://www.last.fm/tag/dance"},{"name":"cher","url":"https://www.last.fm/tag/cher"},{"name":"albums I own","url":"https://www.last.fm/tag/albums+I+own"}]},"wiki":{"published":"27 Jul 2008, 15:55","summary":"Believe is the twenty-third studio album by American  singer-actress Cher, released on November 10, 1998 by Warner Bros. Records. The RIAA certified it Quadruple Platinum on December 23, 1999, recognizing four million shipments in the United States; Worldwide, the album has sold more than 20 million copies, making it the biggest-selling album of her career. In 1999 the album received three Grammy Awards nominations including \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\" for the single \"Believe\". <a href=\"http://www.last.fm/music/Cher/Believe\">Read more on Last.fm</a>.","content":"Believe is the twenty-third studio album by American  singer-actress Cher, released on November 10, 1998 by Warner Bros. Records. The RIAA certified it Quadruple Platinum on December 23, 1999, recognizing four million shipments in the United States; Worldwide, the album has sold more than 20 million copies, making it the biggest-selling album of her career. In 1999 the album received three Grammy Awards nominations including \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\" for the single \"Believe\".\n\nIt was released by Warner Bros. Records at the end of 1998. The album was executive produced by Rob Dickens. Upon its debut, critical reception was generally positive. Believe became Cher's most commercially-successful release, reached number one and Top 10 all over the world. In the United States, the album was released on November 10, 1998, and reached number four on the Billboard 200 chart, where it was certified four times platinum.\n\nThe album featured a change in Cher's music; in addition, Believe presented a vocally stronger Cher and a massive use of vocoder and Auto-Tune. In 1999, the album received 3 Grammy Awards nominations for \"Record of the Year\", \"Best Pop Album\" and winning \"Best Dance Recording\". Throughout 1999 and into 2000 Cher was nominated and winning many awards for the album including a Billboard Music Award for \"Female Vocalist of the Year\", Lifelong Contribution Awards and a Star on the Walk of Fame shared with former Sonny Bono. The boost in Cher's popularity led to a very successful Do You Believe? Tour.\n\nThe album was dedicated to Sonny Bono, Cher's former husband who died earlier that year from a skiing accident.\n\nCher also recorded a cover version of \"Love Is in the Air\" during early sessions for this album. Although never officially released, the song has leaked on file sharing networks.\n\nSingles\n\n\n\"Believe\"\n\"Strong Enough\"\n\"All or Nothing\"\n\"Dov'è L'Amore\" <a href=\"http://www.last.fm/music/Cher/Believe\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."}}}

Would anybody be able to show where I may be going wrong?

Many thanks,

Ben

  • You are not using your second `getJSON`?! It has an empty function. Put a `console.log(data)` in it, what happens then? – ferikeem May 21 '21 at 11:50
  • @ferikeem I have changed `function(data)` to`console.log(data)` which seems to show that the second ajax call is doing nothing... – Ben Brooks May 21 '21 at 12:56

1 Answers1

0

Use encodeURIComponent in the query string

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=cf59c4a923ea2f7743bd8899ec439581&artist=" + release.basic_information.artists[0].name + "&album=" + encodeURIComponent(release.basic_information.title) + "&format=json", 
                    function(data){
                    });
Chris Wong
  • 564
  • 4
  • 4
  • I have added this to both references, which will fix any potential issues I could have, however, I still can't call anything from the second getJSON using data.artist or the like. Thank you! – Ben Brooks May 21 '21 at 12:54
  • Have you open Chrome DevTools to see the network? – Chris Wong May 23 '21 at 00:50
  • I have and I can see it there, getting all the information from last.fm. Just for some reason it returns that data.album.name is not defined – Ben Brooks May 24 '21 at 08:55
  • The variable `content` should be placed in the callback of second $.getJSON, currently it is placed out of it. In addition, I suggested the 2 callback function should have different variable names such as data1, data2. – Chris Wong May 26 '21 at 06:13
  • You're 100% right, everything has now been fixed! Just spent too long looking at the code for things to make sense. – Ben Brooks May 26 '21 at 12:32
  • It is glad to make useful suggestion. – Chris Wong May 27 '21 at 01:19