在服务器端,需要使用request.setCharacterEncoding("utf-8");去解码即可。
注意:该方法必须放在代码处理的第一行。
而firefox 会使用"utf-8"来编码。
解决方式:
step1
找到 tomcat的server.xml文件(TOMCAT_HOME/
conf/server.xml)。添加"URIEncoding="utf-8";
作用是告诉服务器,对亍get请求中的数据,使用"utf-8"解码。
step2
对请求地址使用encodeURI()函数进行处理,该函数的作用是: 对请求地址中的中文进行 "utf-8"编码。
例如:
function check_user(){
//step1 得到XmlHttpRequest对象
var xhr=getXmlHttpRequest();
//step2 发送请求
var uri='checkUser.do?username='+$("#userId").val();
xhr.open('get',encodeURI(uri),true);
//step4 获取服务器返回的数据,更新页面
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
//获取返回的文本信息
var txt=xhr.responseText;
alert('校验成功');
}else{
alert('校验失败');
}
}else{
alert('正在校验……');
}
}
//step3 发送数据
xhr.send(null);
}