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

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

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);
    }

 

转载于:https://www.cnblogs.com/z-yy/archive/2012/09/07/2674741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值