Last.fm api和&字符

时间:2011-03-31 17:31:26

标签: javascript api last.fm

var artist = 'Marina & the Diamonds';
var infourl = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='+artist+'&api_key=xxx&format=json&callback=?';

这是我的javascript代码,我使用infourl进行getJson查询。但正如你所看到的,艺术家变量有一个&字符,而last.fm api正在返回名为Marina的艺术家,而不是Marina&钻石。同时,网址似乎很好:

Request URL:http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Marina%20&%20the%20Diamonds&api_key=xxx&format=json&callback=jsonp1301591978245

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:5)

网址&已编码为&

http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Marina%20&%20the%20Diamonds&api_key=xxx&format=json&callback=jsonp1301591978245

应为%26

http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Marina%20%26%20the%20Diamonds&api_key=xxx&format=json&callback=jsonp1301591978245

使用encodeURIComponent("Marina & the Diamonds")对查询字符串参数进行编码。