zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    2019-9-2-win10-uwp-Markdown
    2018-8-10-控件
    2018-8-10-win10-uwp-dataGrid
    2018-2-13-win10-uwp-hashcash
    2018-2-13-git-cannot-lock-ref
    UCOSIII系统内部任务
    UCOSIII时间片轮转调度
    Keil MDK fromelf生成bin文件
    UCOS内存管理
    uavcan扩展帧格式 zubax
  • 原文地址:https://www.cnblogs.com/AnXinliang/p/5726549.html
Copyright © 2011-2022 走看看