jquery ajax 传数据到后台乱码的处理方法

来源:互联网 发布:神仙水 去痘印 知乎 编辑:程序博客网 时间:2024/05/23 12:01

数据传递之前,先对中文进行编码,如下红色字体:

复制代码
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);    }
复制代码
0 0
原创粉丝点击