您的位置:首页 > 其它

ajax中文乱码问题解决方案

2017-12-17 05:59 417 查看
在使用ajax发送请求给后台时,出现了中文乱码问题,搜索了很久之后得出最好的解决方法:encodeURIComponent,用法示例如下

$.ajax({
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data:{
"keyword":encodeURIComponent(keyword),
//这里用encodeURIComponent来解决中文乱码问题,原因尚且不知,但是不添加的话会乱码
"page":searchPage
},
url:"something.do",
success:function(data){
console.log(songList);
}
});

encodeURIComponent方法将字符串转义,官方解释如下:
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: