0

I am new to coding, so this might be a simple fix, but I am not sure what I did wrong. I am building a program that uses Spotify's API.

I'm not going to lie, this isn't a super useful program haha, but what you do is you put in an ID value for the artist and I want it to return a list of all of the albums. Here is an example of an id: 4P0dddbxPil35MNN9G2MEX

This is the error message that I get:

https://api.spotify.com/v1/artists/undefined/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400 (Bad Request).

Does anyone know why my search result comes up as "undefined?"

Here is my JavaScript/jQuery:

var showArtistInformation = function(artistSelect){
    //clone the result template
    var artistResult = $('.searchResults .hiddenTemplates .artistAlbumInformation').clone();
    //Set the name of the artist in the result
    var theArtistName = result.find('.artistName');
    theArtistName.text(artists.name);
    //Get the name of the artist Album
    var theAlbumName = result.find('albumName');
    theAlbumName.text(album.name);
    //Get the genre of the album
    var theAlbumGenre = result.find('albumGenre');
    theAlbumGenre.text(genre.name);
    //Get the tracklisting for each Album
    var theTrackNames = result.find('trackList');
    theTrackNames.text(track.name);

    return artistResult;
}

    var getArtistAlbumInformation = function(artistID){
        //the parameters that we need to pass in our request to Spotify's API
        var request = {
            album_type: "album"
        };

        $.ajax({
            url: "https://api.spotify.com/v1/artists/" + artistID + "/albums",
            data: request,
            dataType: "jsonp",
            type: "GET",
        })
        //What the function should do if successful
        .done(function(result){
            $.each(result.items, function(i,item){
                var artistReturnedResult = showArtistInformation(item);
                $('.artistAlbumInformation').append(artistSelect);

            })

        })
        //What the function should do if it fails
        .fail(function(jqXHR, error){
            var errorElem = showError(error);
            $('.search-results').append(errorElem);
        });


    }

//Clicking the submit button activates the function that gets the results
$(document).ready(function() {

    /*Clicking the Search Button*/
        $('.searchButton').on('click',function(event){
            event.preventDefault();
            //Zero out results if previous results have run
                $('.albumArtistInformation').html('');
            //End User Input
            var userSearch = $(this).find("input[name='musicalArtist']").val();
            getArtistAlbumInformation(userSearch);


        });



})

3 Answers3

1

https://api.spotify.com/v1/artists/undefined/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400

Emphasis mine. The artist ID you are giving your function is coming through as undefined. That means that wherever you are calling this function from is where your error is. Try console.log(artistID); in your code to see it yourself.

var userSearch = $(this).find("input[name='musicalArtist']").val(); is where your problem is.

You are selecting by using $(this) which in the context of the click function is the actual search button. So you are trying to find musicalArtist inside your button which makes no sense.

Try using this instead:

var userSearch = $("input[name='musicalArtist']").val();

Just make sure you don't have more than one input with that name as that selector could return multiple elements.

Darko
  • 38,310
  • 15
  • 80
  • 107
  • Refused to execute script because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled. That is the error message that I got. – Chicken Sandwich No Pickles Dec 11 '15 at 02:57
  • aha, it seems that your original problem is now solved and you've gotten to the next one. More info here: http://stackoverflow.com/questions/24528211/refused-to-execute-script-from-because-its-mime-type-application-json-is – Darko Dec 11 '15 at 02:59
  • `dataType: "jsonp",` is your problem in that case, you want `dataType: "json",` – Darko Dec 11 '15 at 03:00
  • Awesome, my problem is fixed. I have another error message, but I think I can take care of this part. This will give me something to play with for a while. Thank you for all of your help guys! – Chicken Sandwich No Pickles Dec 11 '15 at 03:02
1

Change the jQuery find to:

var userSearch = $("input[name='musicalArtist']").val();

this was referring to the button where the click event fired.

Lucas Rodriguez
  • 1,203
  • 6
  • 15
0

If you want to get the album id you need to use the url https://api.spotify.com/v1/albums/AlbumID and if you want to get the artists info then use the url https://api.spotify.com/v1/artist/ArtistID right now you are defining an undefined artist and then putting albums after, also I have tried using this id for both of these and the id is invalid so you need to find the correct id here is a working url to find an album click on this and you will see a valid response https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj?callback=jQuery111104149643396958709_1449799723013&&type=album

Steve K
  • 8,505
  • 2
  • 20
  • 35
  • If you click on the last link that you posted, they have "Album_type" not "type" so are you sure? I'm new with this stuff and I'm just curious. – Chicken Sandwich No Pickles Dec 11 '15 at 03:33
  • My fault I just read through the documentation really quick at https://developer.spotify.com/web-api/get-album/ and it turns out that album_type=album is a valid parameter it is the first one they state I must have read right over it. Im sorry I will take that out of my answer type is a valid parameter as well it is one of the last ones they state. But you will need to define either an artist or album at the begining of the url and were it says undifined in your example that is where the id will go – Steve K Dec 11 '15 at 03:38
  • So what you are saying right now is that instead of using Artist ID values, this just searches for album id values, the way it is currently? – Chicken Sandwich No Pickles Dec 11 '15 at 03:47
  • Ya so I see that your are trying to get an artists albums which if you have correctly inserted the artist id in you should be getting a valid response now right here is a url response for the a random artist at https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=14497997230 which is right along the lines of what you want correct at this documentation https://developer.spotify.com/web-api/get-artists-albums/ I had read your original question wrong – Steve K Dec 11 '15 at 03:59
  • Are you getting a valid response now that you have the correct id in place? Or are there still issues – Steve K Dec 11 '15 at 04:00