XMLHTTP uses Unicode encoding to upload data, while the general page uses gb2312, which leads to garbled Characters During page display. when the page is obtained, XMLHttp returns UTF-8 encoding, which causes garbled characters in the display.
One solution is to use encodeURIComponent and modify Content-Type to application/x-www-form-urlencoded to uniformly encode data into url format. Of course, you can also specify encoding, for example, "application/x-www-form-urlencoded; charset = UTF-8 ",
One solution
Example:Copy codeThe Code is as follows: http_request = new ActiveXObject ("Msxml2.XMLHTTP ");
Http_request.setrequestheader ("content-type", "application/x-www-form-urlencoded; charset = UTF-8 ");
Solution 2
Is to display the declaration as GB2312 in the PHP FileCopy codeThe Code is as follows: header ("Content-Type: text/html; charset = GB2312 ");
Transcode the Chinese characters sent to the server.
As follows:Copy codeThe Code is as follows: <? Php
Header ("Content-Type: text/html; charset = GB2312 ");
If ($ _ POST ['content'])
{
$ _ POST ["content"] = iconv ("UTF-8", "gb2312", $ _ POST ["content"]);
Print ("content is". $ _ POST ['content']);
}
Else
{
Print ("NO content to send ");
}
?>
This can solve the garbled problem.
Ajax Chinese garbled characters in jsp
The parameters in the sending path are in Chinese, and the received parameter values in the server segment are garbled
For example:
Var url = "a. jsp? Name = Xiao Li ";
XmlHTTP. open ("post", url, true );
Solution:
Use the escape () or encodeURI () method provided by javascript
For example:
Client:Copy codeThe Code is as follows: var url = "a. jsp? Name = Xiao Li ";
Url = encodeURI (url );
Url = encodeURI (url); // twice, it is critical [I am not sure why]
/*************************************** *****/
Someone also writes var url = "a. jsp? Name = escape ("Xiao Li ")";
The function is similar to the encodeURI method.Copy codeThe Code is as follows: /*************************************** *****/
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 the sent data
XmlHTTP. open ("post", url, true );
Server:Copy codeThe Code is as follows: String name = request. getParameter ("name ");
Name = java.net. URLDecoder. decode ("name", "UTF-8 ");
2) The returned value of responseText or responseXML contains Chinese characters which are garbled.
Cause: AJAX decodes responseText or responseXML values in UTF-8 format if the data sent by the server segment is not in UTF-8 format, the value of responseText or responseXML may be garbled.
Solution: Specify the data sending format on the server:
In the jsp file:
Response. setContentType ("text/text; charset = UTF-8"); // The txt text file is returned.
Or
Response. setContentType ("text/xml; charset = UTF-8"); // The returned xml file
Gb2312 |
Get data using jquery ajax |
Gb2312 |
Yes |
Jsp: <% @ page contentType = "application/json" pageEncoding = "gb2312" %> json is used for testing, which is the same as text/html. Aps: Response. Charset = "gb2312" (reference network not tested) Php: header ("Content-Type: text/html; charset = gb2312"); (not tested by referring to the network) Html: unsolved |
Gb2312 |
Get data using jquery ajax |
UTF-8 |
No |
|
UTF-8 |
Get data using jquery ajax |
UTF-8 |
No |
|
UTF-8 |
Get data using jquery ajax |
Gb2312 |
Yes |
Jsp: <% @ page contentType = "application/json" pageEncoding = "gb2312" %> json is used for testing, which is the same as text/html. Aps: Response. Charset = "gb2312" (reference network not tested) Php: header ("Content-Type: text/html; charset = gb2312"); (not tested by referring to the network) Html: unsolved |