0

I am working on an web app. that talks to the Last.fm API. It's working fine, except when the artist parameter contains numbers or unusual characters (e.g., "U2", "Ke$ha", etc.) How can I properly encode the parameters?

   for (var item in billboard) {
        track = billboard[item]['song'];
        artist = billboard[item]['artist'];
    } 
    $.getJSON("http://ws.audioscrobbler.com/2.0/?method=track.search&artist=" + artist + "&track=" + track + "&api_key=(myapikey)&format=json&callback=?", function(data) {
           try {
           var matches = data['results']['trackmatches']['track'][0]
           }
           catch(err) {
            returned = data['results']['opensearch:Query']['searchTerms']
            $('#album-display').find('ul').append(returned + "<br>")
           }
           artist = matches['artist']
           track = matches['name']
       });
    } 

var billboard = {
 "5-23-1987": {"artist": "U2", "song": "With Or Without You"},
 "10-15-1988": {"artist": "UB40", "song": "Red Red Wine"},
 "3-7-2009": {"artist": "Flo Rida Featuring Ke$ha", "song": "Right Round"},
 ...
}

1 Answer 1

1

You need to escape the URL in your request, and some of those characters aren't legal or cause issues. Change

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=track.search&artist=" + artist + "&track=" + track + "&api_key=(myapikey)&format=json&callback=?", function(data) {

to

var query = "method=track.search&artist=" + encodeURIComponent(artist) + "&track=" + encodeURIComponent(track) + "&api_key=(myapikey)&format=json&callback=?";
$.getJSON("http://ws.audioscrobbler.com/2.0/?" + query, function(data) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.