1.使用encodeURIComponent(URIstring)
语法 :encodeURIComponent(URIstring)
js:encodeURIComponent不会对ASCII 字母和数字编码,encodeURIComponent可以对参数的中文和特殊字符转义,从而不会引起乱码。encodeURIComponent最常用的是对请求的参数编码,以防止乱码。
示例:
jsp页面:
var url = "http://www.cnblogs.com";
url+= "?params=" + encodeURIComponent(encodeURIComponent(JSON.stringify(options.params)));
Java页面:
URLDecoder.decode(params, "utf-8").replaceAll("\n","").replaceAll("\r","");
说明:options.params在这里是某个对象中的JSON对象。
2.使用new String(“原码”,"目标编码")。
示例:
jsp页面:
$.get(""${rootPath}/asset/assetModify/lotModifyAssetExcelTemplate.export?params="+params.toString()");
java页面:
public void example(HttpServletResponse response,String params) throws Exception {
params = new String(params .getBytes("iso8859-1"),"utf-8");
}