您的位置:首页 > Web前端 > JQuery

jquery,ajax开发中遇到的中文乱码问题

2011-11-02 19:42 393 查看
在js中通过url传值的时候会遇到中文乱码问题,现解决方案如下:

传值的时候用两次encodeURI,在后台接收的时候再用URLDecoder.decode

参考代码:

后台servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String param = request.getParameter("userName");
if (param == null || param.length() == 0) {
out.println("用户名不能为空");
} else {
String userName = URLDecoder.decode(param, "UTF-8");
if (userName.equals("somnl")) {
out.println("用户名[" + userName + "]已被注册");
} else {
out.println("你可以使用[" + userName + "]注册");
}
}
} finally {
out.close();
}
}

前台js:

$(document).ready(function() {

$("#verifyButton").click(function(){
var userName = $("#userName").val();
if(userName == ""){
alert("用户名不能为空")

}else{
$.get("UserVerify?userName=" + encodeURI(encodeURI(userName)),null,function(response){
$("#result").html(response);
});
}

});

$("#userName").keyup(function(){

});

}

);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: