前台传中文到后台,如果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;
}