Because the XMLHTTP uses the Unicode code uploads the data, but the General page uses is the gb2312, this causes the display page to produce the garbled code. And when the page to get the XMLHTTP return is the UTF-8 encoding, which caused the display generated garbled.
One solution is to use encodeURIComponent Plus modified content-type for application/x-www-form-urlencoded "to encode the data in a URL format, and, of course, to specify the encoding, such as:" application/x-www-form-urlencoded; Charset=utf-8 ",
One way to solve this problem
Example: http_request = new ActiveXObject ("Msxml2.xmlhttp"); Http_request.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8");
the solution for the second
is to display the declaration as GB2312 in the PHP file
Header ("content-type:text/html;charset=gb2312");
And for the Chinese to send to the server transcoding.
As follows
The code is as follows |
Copy Code |
<?php Header ("content-type:text/html;charset=gb2312"); if ($_post[' content ')] { $_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]); Print ("Contents is". $_post[' content ')]; } Else { Print ("No content to send"); } ?> |
Therefore, this can solve the garbled problem
Ajax Chinese garbled in JSP
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:
Use the provided escape () or encodeURI () method of JavaScript
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 some people write Var url= "A.jsp?name=escape (" Xiao Li ")";
Features are 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 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 format of UTF-8 decoding, 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:
In the JSP file:
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Or
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
gb2312 |
jquery Ajax Get Data |
gb2312 |
Is |
jsp:<% @page contenttype= "Application/json" pageencoding= "gb2312"%> the JSON used here for testing, and text/html should be the same Aps:response.charset= "gb2312" (reference network not tested) Php:header ("content-type:text/html; charset=gb2312 ");(reference network not tested) HTML: Not resolved |
gb2312 |
jquery Ajax Get Data |
Utf-8 |
Whether |
|
Utf-8 |
jquery Ajax Get Data |
Utf-8 |
Whether |
|
Utf-8 |
jquery Ajax Get Data |
gb2312 |
Is |
jsp:<% @page contenttype= "Application/json" pageencoding= "gb2312"%> the JSON used here for testing, and text/html should be the same Aps:response.charset= "gb2312" (reference network not tested) Php:header ("content-type:text/html; charset=gb2312 ");(reference network not tested) HTML: Not resolved |