AJAX Chinese problems are divided into two categories:
1) The parameters in the sending path are in Chinese, and the parameter values received in the server section are garbled.
For example:
var url="a.jsp?name=Xiao Li";
xmlHTTP.open ("post",url,true);
Solution:
Use the escape() or encodeURI() methods provided by javascript, for example:
Client:
var url="a.jsp?name=Xiao Li";
url=encodeURI(url);
url=encodeURI(url); //Twice, very critical [I don’t know exactly why]
/************************************************/
Some people also write var url="a.jsp?name=escape("Xiao Li")";
The function is similar to the encodeURI method.
/************************************************/
xmlHTTP.setrequestheader("cache-control","no-cache");
xmlHTTP.setrequestheader("Content-Type","application/x-www-form-urlencoded");
xmlHTTP.setrequestheader("contentType","text/html;charset=uft-8")//Specify the encoding format of sent data
xmlHTTP.open ("post",url,true);
Server side:
String name = request.getParameter("name");
name = java.net.URLDecoder.decode("name", "UTF-8");
2) The returned responseText or responseXML value contains Chinese characters and is garbled. Reason: AJAX decodes the responseText or responseXML value according to the UTF-8 format when receiving it. If the data sent by the server segment is not in UTF-8 format, , then the value received by responseText or responseXML may be garbled.
Solution: Specify the format of sending data on the server:
In the jsp file:
response.setContentType("text/text;charset=UTF-8");//What is returned is a txt text file or
response.setContentType("text/xml;charset=UTF-8");//returned xml file
Summary: 1) The format of ajax submitted data is utf-8 by default, using the escape() or encodeURI() method provided by javascript. When receiving on the server side, use java.net.URLDecoder.decode("","UTF -8") method to decode.
2) The default character encoding of the data returned by xtmlhttp is utf-8, so when the server wants to send data to the client, it must also use utf-8 encoding. If the above method still cannot solve the garbled problem, then you can try to convert jsp, htm ,java files are saved in UTF-8 encoding format.
In short: just use UTF-8 encoding for both front and back data interaction.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/jiaoxiaguoke/archive/2009/12/31/5110991.aspx