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

jQuery的ajax传递时乱码解决


Jsp中

var num = $("#txtNum").val();
var name = encodeURIComponent($("#txtName").val());
var className = encodeURIComponent($("#selectClasses option:selected").text());

?

Action中

stu.setNum(URLDecoder.decode(num, "UTF-8"));
stu.setName(URLDecoder.decode(name, "UTF-8"));
stu.setClassName(URLDecoder.decode(className, "UTF-8"));

一般在post中struts过滤器会自动处理。而在get中,需要这样操作。


原理:

encodeURIComponent是js内置函数,是将中文韩文等特殊字符转换成utf-8格式的url编码。

如果给后台传递参数需要使用encodeURIComponent时,需要后台解码对utf-8支持

还要注意的是form中的编码方式和当前页面编码方式相同。