JS傳遞中文字符串或包含中文的字符串到Java后臺時(shí)需要進(jìn)行編碼和解碼的操作骏庸,具體過程如下
首先將需要傳到后臺的字符串在JS代碼中用encodeURI方法進(jìn)行編碼
function encode(id_cn) {
var id = encodeURI(id_cn); // 編碼
location.href = ".../helloWorld/hello/"+id;
};
而后在后臺Java文件中對傳過來的字符串用URLDecoder.decode方法進(jìn)行解碼
public void hello() {
String id = this.getPara();
String id_cn = null;
try {
id_cn = URLDecoder.decode(id, "utf-8"); // 解碼
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
如此便可以在后臺得到中文字符串