前台传中文到后台,如果get提交可能会产生乱码,其中之一解决思路是转码,在前台将中文转unicode码,后台再将unicode转中文,解决乱码
js端代码如下:
function toUnicode(str){
var unicode ="";
for (var i = 0; i < str.length; i++) {
var temp = str.charAt(i);
unicode += '\\u' + temp.charCodeAt(0).toString(16);
}
return unicode;
}
java端代码如下:
public String unicodeToCN(String unicode){
String[] strs = unicode.split("\\\\u");
String returnStr = "";
for (int i = 1; i < strs.length; i++) {
returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
}
return returnStr;
}
解决中文GET请求乱码
本文介绍了一种解决前端向后台GET请求时中文乱码的方法,通过在前端将中文字符转换为Unicode编码,然后在后端接收时再将Unicode转换回中文,有效避免了乱码问题。
1219

被折叠的 条评论
为什么被折叠?



