在自己开发小型服务器时,后端解析post请求中的字符串参数时,中文会乱码。
解决方式:给字符串参数编码成十六进制编码。
function UTF8() {
this.encode = function(string) {
var utftext = "";
var byte = [];
for ( var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);// 获取对应的unicode
if (c < 128) {
utftext += '\\x' + c.toString(16).toUpperCase();// 单字节字符
byte.push(c);
} else {
var byte_count = 2;
if (c > 127 && c < 2048)
byte_count = 2;
else if (c > 2047 && c < 65536)
byte_count = 3;
else if (c > 65535 && c < 1114112)
byte_count = 4;
else
return "编码失败!仅支持4位字节及以下的字符串编码!";
// 生成8字节引用表
var height_code = '';
for ( var j = 0; j < 8; j++) {
if (j < byte_count)
height_code += '1';
else
height_code += '0