escape与encodeURI转码

学习分享2年前 (2022)更新 bestcyt
184 0 0

测试:QQ音乐高音质无损全能解析API接口—>https://api.mlwei.com/music/api/?key=523077333&id=%e6%83%b3%e4%bd%a0%e7%9a%84%e5%a4%9c&type=so&cache=0&size=hq&nu=10


以上链接中:id参数:%e6%83%b3%e4%bd%a0%e7%9a%84%e5%a4%9c—>由 “想你的夜” 转码而成。

$('#btn').on('click', function() {
	var songName = $('input').val();
	//中文转码,url编码
	var result_id = escape(songName);
	var transcoding = encodeURI(songName);
	console.log('歌名escape编码:' + result_id + '-------这个编码直接传到url中解析会导致中文乱码');
	console.log('歌名encodeURI编码:' + transcoding + '-------这个编码就正常');
	$.ajax({
		type: "get",
		url: "https://api.mlwei.com/music/api/?key=523077333&id=" + transcoding + "&type=so&cache=0&size=flac&nu=10",
		success: function(data) {
			console.log(data);
		}
	});
})

用 encodeURI 编码结果为正常:
escape与encodeURI转码


用 escape 编码结果为乱码:
escape与encodeURI转码

© 版权声明

相关文章