jQuery发请求传输中文参数乱码问题的解决方案
最近在做的需求,涉及到级联查询,需要根据上级下拉框的内容,查询出下级下拉框列表,因为级联只有两级,且后期对表中的数据几乎不会改动,所以我设计的表存的直接是中文。
菜单如下:
代码如下:
var url = "${basePath}/institutionConfig/getDepartmentByCenter.do?param=" + center; $.get(url, function (data) { var list = data.data; for (var i = 0; i < list.length; i++) { departmentSelector += "<option value='" + list[i] + "' "; if (department && list[i] == department) { departmentSelector += "selected='selected'"; } departmentSelector += ">" + list[i] + "</option>"; } $("#accountDepartmentAdd").html(departmentSelector); });
我用的是$.get(url, callback)
向后台发送请求的,由于将参数直接以GET方式发送,所以浏览器对参数用URL编码进行了encode,而后台获取到的参数为:
可以看到,param接收到的是乱码。所以我进行了进一步的处理,即转码:
String center = new String(param.getBytes("iso8859-1"), "utf-8");
这样接收到的就是中文了。
但是这样的做法在测试环境竟然报错了,分析了下原因,发现测试环境接收到的就是正确的中文,转码后反而错了。所以,解决方案应该是改页面的请求。因为使用了GET方式导致的参数被编码了,所以改为POST请求,POST请求会提交原始数据:
var url = "${basePath}/institutionConfig/getDepartmentByCenter.do"; $.ajax({ url: url, data: {"param": center}, dataType: "json", type: "POST", success: function (data) { var list = data.data; for (var i = 0; i < list.length; i++) { departmentSelector += "<option value='" + list[i] + "' "; if (department && list[i] == department) { departmentSelector += "selected='selected'"; } departmentSelector += ">" + list[i] + "</option>"; } $("#accountDepartmentAdd").html(departmentSelector); } });
总结
相关推荐
ppsurcao 2020-06-14
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18