【www.gdgbn.com--页面特效】

php教程+ajax乱码解决方法
/*
出现乱码原因可以就是php处理页面与发送数据的页面编码不一致,我们只要设置一致就OK了。
下面看个实例

*/

header("Content-Type=text/html;charset=gbk;");
?>
<script>
var xmlHttp=false;
var requestType="";
function createXMLHttpRequest()
         {
             if(window.ActiveXObject)
                    {

  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
else if(window.XMLHttpRequest)
{
  xmlHttp=new XMLHttpRequest();
}
}
function queryCity(citycode){
createXMLHttpRequest();
type="city";
var url="data.php?provincecode="+encodeURI(encodeURI(citycode));

//alert(url);
xmlHttp.open("GET",url,true);
  xmlHttp.onreadystatechange=handleStateChange;

xmlHttp.send(null);
}
</script>

php处理页面

$pro=urldecode($_GET["provincecode"]);
$pro=iconv("utf-8","gbk",$pro);

这样会出现返回数据是乱码,那我们只要在php处理页面加
header("Content-Type=text/html;charset=gbk;");或者
header("Content-Type=text/html;charset=utf;");
或者


就OK了。

本文来源:http://www.gdgbn.com/wangyetexiao/25172/