AJAX傳值時採用的是UTF-8編碼格式,客戶端中文字符傳輸到服務器端時,如果服務器編碼格式或者所採用的MVC框架的編碼格式不是UTF-8,則很可能會出現中文亂碼。解決辦法如下:
客戶端用js函數encodeURI()對中文字符進行兩次編碼,服務器端採用URLDecoder類對客戶端傳輸過來的中文字符進行UTF-8格式的解碼。示例:
$.ajax({
type: "post",
url: "createNewGroup.action",
data:"name="+encodeURI(encodeURI("張三")),
success: function(msg){
alert(msg);
}
});
服務器端代碼:String name = URLDecoder.decode("客戶端傳輸過來的中文字符","UTF-8");
服務器端往客戶端傳輸中文字符出現亂碼時,服務器端可採用URLEncoder類對中文字符進行UTF-8格式的編碼。客戶端採用js函數decodeURI()對服務器端傳輸過的中文字符進行兩次解碼。
當服務器端傳輸的是一個json串時,且不可對整個json串進行UTF-8格式的編碼(編碼後的json串有可能不再具有json原有格式),可採用JsonValueProcessor接口和JsonConfig類對json串中的value進行單獨編碼。
示例代碼:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(String.class,
new JsonValueProcessor(){
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
/**
* process
* @param value
* @return
*/
public Object process(Object value) {
try {
if (value instanceof String) {
return URLEncoder.encode(value.toString(),"UTF-8");
}
return value == null ?"" : value.toString();
} catch (Exception e) {
return "";
}
}
});
JSONArray json = JSONArray.fromObject("[{name:\"張三\";age:\12\";sex:\"男\"}]",
jsonConfig
); //編碼後的json串
[java] view plaincopyprint?
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(String.class,
new JsonValueProcessor(){
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
/**
* process
* @param value
* @return
*/
public Object process(Object value) {
try {
if (value instanceof String) {
return URLEncoder.encode(value.toString(), "UTF-8");
}
return value == null ? "" : value.toString();
} catch (Exception e) {
return "";
}
}
});
JSONArray json = JSONArray.fromObject("[{name:\"張三\";age:\12\";sex:\"男\"}]",
jsonConfig
); //編碼後的json串
客戶端使用函數decodeURI()再對json串中的value值進行兩次解碼即可。
針對這種問題的解決方案,我總結大致有兩種:
一,在瀏覽器端對要傳遞的中文參數進行編碼處理.代碼如下:
xmlhttp.open("POST","AjaxServlet",true); //請求參數初始化
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //因爲請求方式爲POST,所以這裏要設置請求頭.(如果請求方式爲GET,此句代碼可以省略)
xmlhttp.send("name="+encodeURI(encodeURI("中國"))); //向服務器端發送參數
在服務器端代碼:
PrintWriter out = response.getWriter(); //得到response的輸出流對象
String name1 = request.getParameter("name"); //得到KEY爲"name"的請求參數
String name = URLDecoder.decode(name1,"utf-8"); //對得到的參數進行解碼
out.print(name); //向瀏覽器端發送數據
二,在瀏覽器端對要傳遞的中文參數進行編碼處理.代碼如下:
xmlhttp.open("POST","AjaxServlet",true); //請求參數初始化
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //因爲請求方式爲POST,所以這裏要設置請求頭.(如果請求方式爲GET,此句代碼可以省略)
xmlhttp.send("name="+encodeURI("中國")); //向服務器端發送參數
在服務器端代碼:
PrintWriter out = response.getWriter(); //得到response的輸出流對象
String name1 = request.getParameter("name"); //得到KEY爲"name"的請求參數
String name = new String((name1.getBytes("ISO-8859-1")),"UTF-8"); //對得到的參數進行解碼
out.print(name); //向瀏覽器端發送數據
以上兩種方法,在使用XMLHttpRequest對象傳遞中文參數時,亂碼問題可以在任意瀏覽器下解決,正確顯示中文.
原文出處已經不記得了,希望作者能夠見諒,