天天看点

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中的编码方式和当前页面编码方式相同。