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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM