Recently in a project, encountered Ajax Chinese garbled problem, after an afternoon of efforts to finally perfect solution, now will write down, hope to those who still trapped around this problem and headache people have helped.
As we all know, the use of Ajax transmission and reception of Chinese parameters, if not the client and the server do the corresponding processing will appear garbled problem, in the online corresponding article also many, but some cases difficult to find in line with their ideal answer, I today is on the internet to find a lot, but almost, Talk about ASP and JSP more (I am using PHP), so in the end did not find their own satisfactory answer.
Ajax Chinese garbled can be divided into two, the first is to send a Chinese parameter to the server (Xmlhttp.open ("Get|post", Url,true)), the server received the garbled, this is also the problem I encountered today, did not do the processing before, in IE is normal , but in Firefox there is garbled, I first took the parameter output to a text, no problem found, depressed, and then I put the query in the output to observe (I am here to find out from the database and parameters related 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 the connection and the front and back, so that the identification is a coding problem, in the Internet to find relevant information, have not solved the problem, but to get some inspiration, because the AJAX send data are sent by UTF-8 encoding, So to the server-side encoding conversion (I am here to use GB2312 encoding, if the use of UTF-8 should not have this step of the problem), so I on the server side of the UTF-8 to GB2312,
$str =iconv ("UTF-8", "GB2312", $str);
Then the test, in Firefox successfully solved the problem, thought the big announcement became, but then to the IE test, found that there is a problem with IE, the server side received the parameter is not value, this depressed, suddenly see the sending head set up setRequestHeader (" Content-type "," application/x-www-form-urlencoded "); Find the problem, then the parameter code is sent there:
Geturl=encodeuri (Geturl);
Geturl=encodeuri (Geturl); Two times can also be written Geturl=encodeuri (encodeURI (Geturl));
Xmlhttp.open ("GET", geturl,true);
Then go 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, otherwise it will not get the correct value
Save the test, IE and Firefox will be OK.
The second is the server side to the client output Chinese garbled, this kind of problem online answer more, also can solve, in order to avoid everyone again to find, I here on the copy under J
Reason: Ajax in receiving 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, It is possible that the value of receiving responsetext or responsexml is 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");//The XML file returned
Php:header (' content-type:text/html;charset=gb2312 ');
ASP:Response.Charset ("GB2312")
JSP:response.setHeader ("Charset", "GB2312");
Excerpt from Hurry's column
http://www.bkjia.com/PHPjc/478467.html www.bkjia.com true http://www.bkjia.com/PHPjc/478467.html techarticle recently in a project, encountered Ajax Chinese garbled problem, after an afternoon of efforts to finally perfect solution, now will write down the experience, hope for those still trapped around this problem and headache unceasingly ...