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 查询。但正如您所看到的,artist 变量有一个 &
字符,last.fm api 返回名为 Marina 的艺术家,而不是 Marina & the Diamonds。同时,该网址似乎没问题:
Request URL:http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Marina%20&%20the%20Diamonds&api_key=xxx&format=json&callback=jsonp1301591978245
有什么想法吗?谢谢
最佳答案
URL 将 &
编码为 &
。
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")
对查询字符串参数进行编码。
关于javascript - Last.fm api 和 & 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503869/