首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

jQuery的ajax传送时乱码解决

2012-09-19 
jQuery的ajax传递时乱码解决Jsp中var num $(#txtNum).val()var name encodeURIComponent($(#txtNa

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

热点排行