解決Json傳輸中文亂碼問題


1、如果是通過URL傳遞:----需要編碼兩次

var searchText = this.searchText();
searchText = encodeURI(searchText);
searchText = encodeURI(searchText);
$.ajax({
type: 'GET',
url: $ctx + this.pageUrl + pageIndex + "&searchText=" + searchText,
data: '',
contentType: 'text/json,charset=utf-8',
dataType: 'json',
success: function(data) {
}
})
},

后台通過:

String queryCon = request.getParameter("searchText");
if(queryCon != null && queryCon != ""){
queryCon=URLDecoder.decode(queryCon,"utf-8");
}

反編譯一下就可以獲取到傳遞的中文~~

2、  直接通過ajax數據傳遞:只需編譯一次~

var searchText = this.searchText();
searchText = encodeURI(searchText);
$.ajax({
type: 'GET',
url: $ctx + this.pageUrl + pageIndex ,
data: {search:searchText },
contentType: 'text/json,charset=utf-8',
dataType: 'json',
success: function(data) {
}
})
},

后台直接獲取到傳遞的值,需要解碼一次:

String queryCon = search;
if(queryCon != null && queryCon != ""){
queryCon=URLDecoder.decode(queryCon,"utf-8");
}


免責聲明!

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



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