Solution to Chinese garbled characters when applying AJAX in JAVA: 1. Use the escape or encodeURI method when submitting ajax, and must use it twice; 2. Use the decode method of the [java.net.URLDecoder] class in background java .
【Related learning recommendation: java basic tutorial】
Chinese garbled code for applying AJAX in JAVA Solution:
1. Use the escape or encodeURI method when submitting ajax. Must be used twice
2. The background java uses the decode method of the java.net.URLDecoder class.
Foreground page partial code:
/** * marging是一个js对像用于承载页面部分功能和参数 * xmlHttp是改写后的AJAX对像,属性paramString为post方式发送到服务端的参数 * 前台,后台页面都采用的GBK编码,使用了编码过滤器 */ xmlHttp.paramString="mName="+marging.mNames; //参数经过2次encodeURI编码 xmlHttp.paramString=encodeURI(xmlHttp.paramString); xmlHttp.paramString=encodeURI(xmlHttp.paramString); //发送保存请求 var xmlReq=xmlHttp.getTextByPost("mailMerginged.jsp");
Backend page code:
/** * 当调用request.getParameter()函数时,会自动进行一次URI的解码过程 * 调用时内置的解码过程会导致乱码出现。而URI编码两次后,request.getParameter()函数 * 得到的是原信息URI编码一次的内容。再用可控的解码函数java.net.URLDecoder.decode() * 就可解出原始的正确的信息。 */ String mName=java.net.URLDecoder.decode(request.getParameter("mName"),"UTF-8");
Recommended related articles: ajax video tutorial
The above is the detailed content of How to deal with Chinese garbled characters when applying AJAX in JAVA. For more information, please follow other related articles on the PHP Chinese website!