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

时间:2023-07-15 13:26:44

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

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

     /*
*掩码处理
*/
function maskWord() {
var code=encodeURI($("#maskword").val());
code=encodeURI(code);
$.ajax({
url : '${maskWordurl}' + code,
type : 'post',
dataType : 'json'
}).done(function(data, status, xhr) {
$("#maskword").val(data.msg);
}).fail(function(xhr, status, error) {
alert("失败!");
});
} </script>
</head>
<body>
<form id="form1" runat="server" method="post" accept-charset="utf-8" >
<div>
<input id="maskword" style="width: 500PX "/><input type="button" value="掩码" text="掩码" onclick="maskWord();"/>
</div> </form>
</body>
</html>

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

     /*
* 掩码处理
*/
@RequestMapping(value = "/maskWord/{code}")
public @ResponseBody Object maskWord(@PathVariable("code") String code) throws SQLException, UnsupportedEncodingException {
code =java.net.URLDecoder.decode(code, "utf-8");
String rec = Common.getTelnum(code);
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", rec);
return jsonObject;
}