Because XMLHTTP in the processing of the return of the Responsttext Responstbody by the UTF-8 encoding decoding, if the server-side sent the data stream is really UTF-8 code, then the Chinese text will be correctly displayed, However, this is the case if it is GBK or other coding.
Ajax Chinese garbled can be divided into two, the first is to send Chinese parameters to the server (Xmlhttp.open ("Get|post", url,true), server-side received for garbled, this is the problem I encountered today, did not do processing, in IE is normal , but in Firefox there is garbled, I first received the parameters output to a text, did not find any problems, depressed, and then I will query statements in the output to observe (I am here to find out from the database with the parameters related to things), and finally found the problem, IE and Firefox output parameters are not the same, although the Chinese characters are the same, but there is a small difference between and before and after the connection, it was identified as a coding problem, the Internet to find the relevant information, have not been able to solve the problem, but get some inspiration, because the AJAX send data are sent in the UTF-8 encoding way, So in the server to encode the conversion (I am here the page is GB2312 encoded, if it is the use of UTF-8 words should not have this step of the problem), so I UTF-8 to GB2312 in the server side,
$str =iconv ("Utf-8″," Gb2312″, $STR);
Then the test, in the Firefox smooth solved the problem, thought big announcement became, but again to IE test, found that IE again appeared the problem, the server received the parameter is not worth, this depressed, suddenly see the Send head set setRequestHeader (" Content-type "," application/x-www-form-urlencoded "), find out where the problem is, and then encode the parameters in the send:
The code is as follows |
Copy Code |
Geturl=encodeuri (Geturl); Geturl=encodeuri (Geturl); Two times can also be written as Geturl=encodeuri (encodeURI (Geturl)); Xmlhttp.open ("Get", geturl,true); And then to the server side for URL decoding: $str =urldecode ($STR); Decoding $ str =iconv ("Utf-8″," gb2312″,$ str); Encoding Conversion
|
Note: The decoding must precede the encoding conversion or not get the correct value
Save the test, IE and Firefox can be normal.
The second is the server to the client output Chinese when garbled, such problems on the Internet more than the answer, but also can be resolved, in order to avoid everyone again to find, I am here on the copy under J
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.
Ajax garbled Solution one :
Specify the format for sending data on the server:
In the JSP file:
The code is as follows |
Copy Code |
Response.setcontenttype ("Text/text;charset=utf-8″)"//returns TXT text file Or Response.setcontenttype ("Text/xml;charset=utf-8″)"//returned XML file Php:header ("content-type:text/html;charset=gb2312");
|
Ajax garbled solution two :
PHP send Chinese, Ajax receive
Just add a sentence to the top of PHP:
The code is as follows |
Copy Code |
Header (' content-type:text/html;charset=gb2312′);
|
XMLHTTP will correctly parse the Chinese in them.
Ajax send Chinese, PHP receive
This is more complicated:
In Ajax, first use encodeURIComponent to encode the Chinese to be submitted
In PHP:
The code is as follows |
Copy Code |
$GB 2312string=iconv (' utf-8′, ' Gb2312//ignore ', $RequestAjaxString); PHP writes MySQL database When you set up conn, query the set names "Gb2312″
|
==================================================
Note the red part!!! Otherwise you cannot use!!! Specific reason to guess yourself!
The code is as follows |
Copy Code |
Iconv ("Utf-8″", "GBK", $gametypes ["name"]); |
Original:
My garbled problem is the AJAX data generated garbled, the original page is the use of GBK code. Later displayed as garbled, I use UE to convert it into UTF-8,
The original elements on the page can be displayed correctly. However, the value of the database is still garbled.
Ob_get_contents ();
Remove all the display data, and then output to the log file, the display is all garbled.
Using the UE to convert the file to GBK and then output, the display is correct.
Here again use:
The code is as follows |
Copy Code |
Header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');
|
The foreground page appears as normal.
With all this talk, let's look at the examples.
For example:
The code is as follows |
Copy Code |
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:
The code is as follows |
Copy Code |
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:
The code is as follows |
Copy Code |
String name = Request.getparameter ("name"); Name = Java.net.URLDecoder.decode ("name", "UTF-8"); |
The easiest way to summarize this is to have the page and the page code returned by the request are consistent.
The solution of Ajax Chinese garbled, in fact, will be in the server side of the file plus a header, indicating what the code stream is sent. Like what
An ASP document adds a single line to the header description:
The code is as follows |
Copy Code |
Response.Charset ("GB2312") |
Examples of PHP are as follows:
The code is as follows |
Copy Code |
Header (' content-type:text/html;charset=gb2312 '); |
The JSP is as follows:
code is as follows |
copy code |
response.setheader ("Charset", "GB2312"); |