数据传递之前,先对中文进行编码,如下:
function saveCommentTemplate()
{
$.ajax({
cache : false,
type:'get',
dataType:'json',
url:'comment/insert',
contentType:'application/json;charset=UTF-8',
data:{name:encodeURI($("#name").val()),
content:encodeURI($("#content").val())},
success:function(data){
alert("ok")
},
error: function() {
alert("error")
}
});
$("#bottom").hide();
}
等数据传过来时,再对数据进行解码:
@RequestMapping(value = "insert")
@ResponseBody
public void insert(@RequestParam("name") String name,@RequestParam("content")String content) throws UnsupportedEncodingException
{
name=URLDecoder.decode(name,"UTF-8");
content=URLDecoder.decode(content,"UTF-8");
commentTemplateService.saveCommentTemplate(name,content);
}
本文详细介绍了在数据传递过程中,如何通过编码和解码操作来确保中文字符在JSON格式的数据交换中不失真,并成功实现数据安全传输。
259

被折叠的 条评论
为什么被折叠?



