springmvc框架下ajax请求传参数中文乱码解决

springmvc框架下jsp界面通过ajax请求后台数据,传递中文参数到后台显示乱码

解决方法:js代码 运用encodeURI处理两次

 1     /*
 2      *掩码处理
 3      */
 4     function maskWord() {
 5         var code=encodeURI($("#maskword").val()); 
 6         code=encodeURI(code);
 7         $.ajax({
 8             url : '${maskWordurl}' + code,
 9             type : 'post',
10             dataType : 'json'
11         }).done(function(data, status, xhr) {
12             $("#maskword").val(data.msg);
13         }).fail(function(xhr, status, error) {
14             alert("失败!");
15         });
16     }
17         
18         
19 </script>
20 </head>
21 <body>
22     <form id="form1" runat="server"  method="post" accept-charset="utf-8" >    
23         <div>
24             <input id="maskword" style=" 500PX "/><input type="button" value="掩码" text="掩码" onclick="maskWord();"/> 
25         </div>
26         
27     </form>
28 </body>
29 </html>

后台java代码  java.net.URLDecoder.decode(code, "utf-8"); 反编码处理

 1     /*
 2      * 掩码处理
 3      */
 4     @RequestMapping(value = "/maskWord/{code}")
 5     public @ResponseBody Object maskWord(@PathVariable("code") String code) throws SQLException, UnsupportedEncodingException {
 6         code =java.net.URLDecoder.decode(code, "utf-8"); 
 7         String rec = Common.getTelnum(code);
 8         JSONObject jsonObject = new JSONObject();
 9         jsonObject.put("msg", rec);
10         return jsonObject;
11     }
原文地址:https://www.cnblogs.com/AnXinliang/p/5726549.html