Home >Backend Development >PHP Tutorial >AJAX Chinese garbled PHP perfect solution (compatible with IE and Firefox)_PHP tutorial
I was working on a project recently and encountered the problem of AJAX Chinese garbled characters. After an afternoon of hard work, I finally solved it perfectly. Now I will write down my experience. I hope it will be helpful to those who are still stuck with this problem and have headaches.
As we all know, when using AJAX to transmit and receive Chinese parameters, if the client and server do not perform corresponding processing, garbled characters will appear. There are many corresponding articles on the Internet, but in some cases it is difficult to find answers that meet your ideals. , I searched a lot on the Internet today, but they were all similar. Most of them talked about ASP and JSP (I used PHP), so I didn't find a satisfactory answer in the end.
Chinese garbled characters in AJAX can be roughly divided into two types. The first one is when sending Chinese parameters to the server (xmlhttp.open("get|post",url,true)). The server receives garbled characters. This is what I The problem I encountered today, before it was dealt with, was normal in IE, but garbled characters appeared in Firefox. I first output the received parameters into a text, and found no problem. I was depressed, and then I Observing the output of the query statement (I am here to find out things related to parameters from the database), I finally found the problem. The parameters output by IE and Firefox are different. Although the Chinese characters are the same, there are small differences in the front and rear connections. difference, so I determined that it was an encoding problem. I searched for relevant information on the Internet, but could not solve the problem. However, I got some inspiration. Because AJAX sends data using UTF-8 encoding, it must be done on the server side. Encoding conversion (my page here is encoded in GB2312, if UTF-8 is used, there should be no problem in this step), so I converted UTF-8 to GB2312 on the server side,
$str=iconv("UTF-8","GB2312",$str);
Then I tested it and successfully solved the problem on Firefox. I thought it was a big announcement. But when I tested it on IE, I found that IE had another problem. The parameters received by the server were worthless. Now I was depressed. Suddenly I saw the message sent. SetRequestHeader("Content-Type","application/x-www-form-urlencoded"); is set in the header, and the problem is found, and then the parameters are encoded where sent:
geturl=encodeURI(geturl);
geturl=encodeURI(geturl); //Twice can also be written as geturl=encodeURI(encodeURI(geturl));
xmlhttp.open("GET",geturl,true);
Then go to the server side for URL decoding:
$str=urldecode($str); //Decode
$ str =iconv("UTF-8","GB2312",$ str); //Encoding conversion
Note: Decoding must precede encoding conversion, otherwise the correct value will not be obtained
Save the test and both IE and Firefox will work normally.
The second type is that garbled characters appear when the server outputs Chinese to the client. There are many answers to this type of problem online, and they can all be solved. In order to avoid you looking for it again, I will COPY it here J
Reason: When AJAX receives the value of responseText or responseXML, it decodes it according to the UTF-8 format. If the data sent by the server segment is not in UTF-8 format, the value of the received responseText or responseXML may be garbled.
Solution:
Specify the format of sending data on the server:
In jsp file:
Response.setContentType("text/text;charset=UTF-8");//returns a txt text file
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");