Ajax Chinese problems fall into two main categories:
1 The parameter in the Send path has Chinese, the parameter value in the server segment is garbled
For example:
var url= "a.jsp?name= Xiao Li";
Xmlhttp.open ("Post", url,true);
Solution:
uses JavaScript's provided escape () or encodeURI () method
For example:
Client:
var url= "a.jsp?name= Xiao Li";
Url=encodeuri (URL);
Url=encodeuri (URL); Two times, very key [specifically why, I don't know]
/********************************************/
also someone wrote 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")//Specifies the encoding format for sending data
Xmlhttp.open ("Post", url,true);
Server side:
String name = Request.getparameter ("name");
name = Java.net.URLDecoder.decode (name, "UTF-8");
2) The value of the returned ResponseText or Responsexml contained in Chinese is garbled
Reason: Ajax in the reception of ResponseText or Responsexml value is in accordance with the UTF-8 format to decode, if the server segment sent data is not UTF-8 format, Then the value of receiving responsetext or responsexml may be garbled.
Workaround: Specify the format for sending data on the server:
on the service side:
Copy Code code as follows:
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
Response.setcontenttype ("Application/json")//Return JSON file
PrintWriter bw = Response.getwriter ()//output to use this class
Bw.write (result);
Bw.flush ();
Bw.close ();
Summary: 1 The format of the AJAX submission data defaults to Utf-8, using JavaScript's provided escape () or encodeURI () method. Use Java.net.URLDecoder.decode when receiving on server ("", " UTF-8 ") method for decoding.
2) Xtmlhttp returns the data the default character encoding is Utf-8, so the server to send data to the client, also to use the Utf-8 encoding
if the above method still can not solve the garbled problem, then you try to save the Jsp,htm,java file in UTF-8 encoding format.
in short: the former background data interaction using UTF-8 encoding on the line.