JQ与JAVA(Java使用GBK编码)使用GET传递参数总是出现乱码,网上找了很多解决办法,比如修改tomcat的sever.xml等都试了但是没有成功,最终使用encodeURI与decode就可以解决乱码问题。下面请看代码:
1、JQ代码:
//注册表单ajax查询用户名是否可用
$('input[name=uname]').blur(function(){
$.ajax({
type:'GET',
url:'<%=basePath%>NameChek.Tao',
dataType:'html',
data:'uname='+encodeURI(encodeURI($(this).val())),
beforeSend:function(XMLHttpRequest)
{
$('#AjaxUname').text('正在查询');
//Pause(this,100000);
},
success:function (response,status,xhr) {
$('#AjaxUname').html(response);
}
});
2、Java sevelet中的代码
import java.net.URLDecoder;
response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("GB2312");
String xuname = request.getParameter("username");
String uname =URLDecoder.decode(xuname,"utf-8");
这样就可以解决乱码问题了