The core of Ajax technology is JavaScript, and JavaScript uses UTF-8 encoding, so when the page uses GBK or other encoding, without encoding conversion, there will be the problem of garbled Chinese. Here are the different test results and garbled solutions for the page using GBK and UTF-8 encoded under IE and FF, respectively, with Get and post methods
Pass Value method |
Client-side encoding |
Server-side encoding |
Ie |
Ff |
Solution |
Get |
UTF-8 |
UTF-8 |
Garbled when receiving parameters passed by $_get |
Normal |
Client Url=encodeuri (URL) |
Get |
GBK |
GBK |
Normal |
Garbled when receiving parameters passed by $_get |
Client Url=encodeuri (URL) Server-side $str =iconv ("UTF-8", "GBK", $str) |
POST |
UTF-8 |
UTF-8 |
Garbled when receiving parameters passed by $_get |
Normal |
Client Url=encodeuri (URL) |
POST
|
UTF-8 |
UTF-8 |
Normal to receive parameters passed by $_post |
Normal to receive parameters passed by $_post |
Recommended way to use |
POST |
GBK |
GBK |
Normal |
Garbled when receiving parameters passed by $_get |
Client Url=encodeuri (URL) Server-side $str =iconv ("UTF-8", "GBK", $str) |
POST |
GBK |
GBK |
Garbled when receiving parameters passed by $_post |
Garbled when receiving parameters passed by $_post |
Server-side $str =iconv ("UTF-8", "GBK", $str) |
The following is the code for the test:
Client index.php
<?php header ("Content-type:text/html;charset=utf-8");?> <input type= "button" value= "Load" id= "Button1" >
<div id= "Div1" >Loading</div> <script type= "Text/javascript" > Window.onload=function () {
document.getElementById ("Button1"). Onclick=function () {var xmlHttp;
var url= "t.php?id= ' Test ' &nu=" +math.random (); Url=encodeuri (URL); Here is the key if (window).
XMLHttpRequest) {xmlhttp=new XMLHttpRequest ();
else {xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function () {if (xmlhttp.readystate==4&&xmlhttp.status==200) {document.ge
Telementbyid ("Div1"). Innerhtml=xmlhttp.responsetext;
} xmlhttp.open ("Get", url,true);
Xmlhttp.send ();
Xmlhttp.open ("POST", "t.php", true);
Xmlhttp.open ("POST", url,true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("id= ' Test ' &nu=" +math.random ());
Xmlhttp.send (); }} </script>
Server-Side t.php
<?php
Header ("Content-type:text/html;charset=utf-8");
echo "This is Test Chinese"; If this is directly to the client output Chinese, not through the $_get or $_post pass parameters
//Only the client and server-side coding consistent, whether UTF-8 or GBK, will not appear garbled
echo $_get[' id '].$_get[ ' Nu '];
Echo iconv ("UTF-8", "GBK", $_get[' id '].$_get[' nu ']);
echo $_post[' id '].$_post[' nu '];
Echo iconv ("UTF-8", "GBK", $_post[' id '].$_post[' nu ']);
? >
Another problem may exist in IE:
The operation could not be completed due to an error c00ce56e
At this point, the header (' Content-type:text/html;charset=utf8 ') is changed to header (' Content-type:text/html;charset=utf-8 ') when the encoding is set.