解决Json传输中文乱码问题

一、若是是经过URL传递:----须要编码两次ajax

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) {
}
})
},json

后台经过:this

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

反编译一下就能够获取到传递的中文~~url

二、  直接经过ajax数据传递:只需编译一次~spa

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) {
}
})
},code

后台直接获取到传递的值,须要解码一次:utf-8

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

相关文章
相关标签/搜索