AJAX Chinese garbled PHP perfect solution (compatible with IE and Firefox ). Recently, I was working on a project and encountered an AJAX Chinese garbled problem. after an afternoon of hard work, I finally wrote down my experiences, I hope to have a headache for those who are still stuck around this problem. Recently, I am working on a project and encounter AJAX Chinese garbled characters. after an afternoon's efforts, I have finally solved the problem perfectly. now I will write down my experiences, I hope it will help those who are still stuck with this problem and have a headache.
As we all know, when AJAX is used to send and receive Chinese parameters, garbled characters may occur if they are not processed on the client and server. There are also many articles on the Internet, however, in some cases, it is difficult to find the desired answer. I have found many answers online today, but they are similar, there are a lot of ASP and JSP (I use PHP), so I didn't find a satisfactory answer at the end.
AJAX Chinese garbled characters can be roughly divided into two types. The first type is when a Chinese parameter is sent to the server (xmlhttp. open ("get | post", url, true), the server receives garbled code, which is also a problem I encountered today, it is normal in IE, but garbled characters appear in Firefox. I first output the received parameters to a text, and no problems are found. I am depressed, then I put the query statement in the output to observe (here I want to find out something related to the parameter from the database), and finally found the problem, IE and Firefox output parameters are different, although the Chinese characters are the same, there is a small difference with the front and back connections. Therefore, it was determined that the encoding problem was caused by the failure to solve the problem by searching for relevant information on the Internet, but it had some inspiration, because AJAX sends data are sent in the form of UTF-8 encoding, so to the server side for encoding conversion (I here the page is using GB2312 encoding, if it is the use of UTF-8 should not have this problem), so I made a UTF-8 on the server to GB2312,
$ Str = iconv ("UTF-8", "GB2312", $ str );
Then the test solved the problem smoothly in Firefox, and thought it was done by the public. However, after testing in IE, I found that the IE encountered another problem and the server received no value for the parameter, this is depressing. suddenly, you can see that setRequestHeader ("Content-Type", "application/x-www-form-urlencoded") is set in the sending header, and the problem is located, then the parameter code is carried out at the sending address:
Geturl = encodeURI (geturl );
Geturl = encodeURI (geturl); // You can also enter geturl = encodeURI (geturl) twice ));
Xmlhttp. open ("GET", geturl, true );
Then perform URL decoding on the server:
$ Str = urldecode ($ str); // decoded
$ Str = iconv ("UTF-8", "GB2312", $ str); // Encode and convert
Note: decoding must be prior to encoding conversion, or the correct value is not obtained.
Save the test. both IE and Firefox are normal.
The second type is garbled characters when the server outputs Chinese characters to the client. There are many answers to these questions on the internet and they can all be solved. to avoid further searching, I will COPY J
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.
Specify the format for sending data on the server:
In the jsp file:
Response. setContentType ("text/text; charset = UTF-8"); // The txt text file is returned.
Response. setContentType ("text/xml; charset = UTF-8"); // The Returned xml file
PHP: header ('content-Type: text/html; charset = GB2312 ');
ASP: Response. Charset ("GB2312 ")
JSP: response. setHeader ("Charset", "GB2312 ");
From Hurry's column