Ajax 中文乱码问题(分两种情况)
1.对于Ajax 发的 post请求,服务端只需要 :
request.setCharacterEncoding("utf-8");
2.对于Ajax 发的get 请求
$("#userName").blur(function(){
$.Ajax({
url:"TestServlet",
type:"get", //请求方式是 get
cache:false,
async:false,
data:{userName: encodeURI($("#userName").val())}, //对含有中文的数据,进行 encodeURI(...)
success:function(data){
alert(data);
}
});
});
//服务端
String username=request.getParameter("userName");
username=URLDecoder.decode(username,"UTF-8"); //解码