如下为表单提交代码:
createXMLHttpRequest(); xmlHttp.onreadystatechange=callback; var url="registercheck?userName="+userName+"&password="+pass+"&realName="+realName+"&sex="+sex+ "&birthday="+date+"&id="+id+"&email="+email+"&tel="+tel+"&address="+address+"&checkstr="+checkstr; xmlHttp.open("post",url); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
其中realName和address可能是中文字符串,提交到servlet插入数据库时发现变成乱码,网上搜索一番,有网友提到对url进行两次编码:
url=encodeURI(encodeURI(url));
再在servlet进行解码还原:
realName=java.net.URLDecoder.decode(realName,"UTF-8");
即可解决问题。