Ajax Chinese garbled problem, after an afternoon's efforts to finally perfect solution, is to write down the experience, I hope for those who still trapped in this problem and headache people have helped.
Recently in doing a project, encountered Ajax Chinese garbled problem, after an afternoon's efforts to finally perfect solution, now will write down the experience, I hope that those who are still trapped in this problem and headache people have helped.
As we all know, the use of Ajax to send and receive Chinese parameters, if not the client and the server to do the corresponding processing will appear garbled problem, the corresponding article on the web is also a lot, but in some cases it is difficult to find the answer to meet their own ideals, I am today is on the internet to find Talk about ASP and JSP more (I am using PHP), so in the end did not find their own satisfaction with the answer.
Ajax Chinese garbled can be divided into two, the first is to send Chinese parameters to the server (Xmlhttp.open ("Getpost", 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 parameter output to a text, did not find any problems, depressed, and then I will be the query statement in the output to observe (I am here to find out from the database related to the parameters of 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:
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.
Solution:
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
Php:header (' content-type:text/html;charset=gb2312 ');
ASP:Response.Charset ("GB2312")
JSP:response.setHeader ("Charset", "GB2312");