1. 程式人生 > >json傳輸數據解決中文亂碼問題

json傳輸數據解決中文亂碼問題

.ajax https servle set encode url todo except uri

1.Ajax在url帶參數(中文):

   encodeURI(encodeURI(expireDesc))//設置編碼

2.後臺接收需要轉碼:

  URLDecoder.decode(expireDesc, "UTF-8") //將接收的參數轉碼

3.例子:
js Ajax:

function exchange(expireDesc){
$.ajax({
type: "post",
url:ctx+"/xxx.do?method=xxx&xxx="+ encodeURI(encodeURI(xxx)),
dataType: "json",
success: function(data){
xxxx=data.code;
}
})

}

後臺接收:

public JsonResult exchange(HttpServletRequest request,String expireDesc) {
String expire="";
try {
xxx = URLDecoder.decode(xxx, "UTF-8");//將接收的參數轉碼,用於解決中文亂碼
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MngCodeEntity list = produceService.exchange(expire);
JsonResult jsonResult = new JsonResult();
jsonResult.setCode(list.getCodeVal());
return jsonResult;
}

json傳輸數據解決中文亂碼問題