记解决jQuery $.getJSON() 传递参数中文乱码问题的方法

来源:互联网 发布:迅龙数据恢复破解方法 编辑:程序博客网 时间:2024/06/06 03:53

这是select下拉菜单 change的时候,要从数据库选取其他输入框的数据,所以用到json传值,但是json传中文值乱码,乱码时的js是这样的,如下:

<script type="text/javascript">      $(function(){          $(".sel").change(function(){              var name=$(".sel").find("option:selected").text();              $.getJSON(                      "getBiaozhun.do",                         {name:name},                      function(data, status) {                           if(status == "success") {                              $(".danbaizhiguobiao").val(data.danbaizhiguobiao);                              $(".suanduguobiao").val(data.suanduguobiao);                              $(".zhifangguobiao").val(data.zhifangguobiao);                              $(".feiruzhigutiguobiao").val(data.feiruzhigutiguobiao);                              $(".junluozongshubiaozhun").val(data.junluozongshubiaozhun);                              $(".dachangjunqunbiaozhun").val(data.dachangjunqunbiaozhun);                          }else{                              console.log("失败");                          }                                                    }                  );          });      })  </script>

但是这样的话传值会乱码,于是在博友的帮助下,终于找到了解决方法,就是在

var name=$(".sel").find("option:selected").text();

之后加一句:

name=encodeURI(name,"utf-8"); 

然后在后台Controller中加入 

String productname=URLDecoder.decode(request.getParameter("name"),"utf-8");

按照提示导入URLDecoder的jar包即可,即可解决中文乱码传值问题