我正在开发一个网络应用程序。与 Last.fm API 对话。它工作正常,除非艺术家参数包含数字或不寻常的字符(例如,“U2”、“Ke$ha”等)。如何正确编码参数?
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"},
...
}