日期:2014-05-16  浏览次数:20295 次

js 向action传中文参数乱码解决
转载:

先对js中的中文进行编码。。。
然后到action去解码就可以了。。。
具体实现。。。
js编码
var comment = document.all.comment.value;
comment  = encodeURI(comment );
comment = encodeURI(comment );//注意要两次

action解码
String comment = request.getParameter("comment ");
try {
comment = java.net.URLDecoder.decode(comment, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
从程序里跟踪变量的值发现action里得到了正确的中文字符,但是最后查询出结果还是空集,在数据库里执行sqlquery发现查询结果空,原来数据库的一个编码属性得设置,即把sqlserver的collation改成CHINESE_PRC_CS_AI就没问题了