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);
    }
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章