首页 > 代码库 > AJAX乱码解决新方法

AJAX乱码解决新方法

用过AJAX的朋友肯定知道javascript是使用UTF-8国际编码,即每个汉字用3个字节来存储,但是这就造成了用AJAX来send数据的时候出现乱码。  
   有一种解决办法就是使用encodeURIComponent加上修改 Content-Type 为 application/x-www-form-urlencoded" 来把数据统一编码成 url 格式,但是这样做有一个弊端,使用php的urldecode根本不能得到正确的文字。  
   另一种方法就是通过vbscript 的写的函数把数据转成gb2312格式的,我个人觉得这种方法比较好。有兴趣的朋友可以到网上去查一下。  
   今天突发奇想,AJAX调用的时候会不会发送cookie 呢?马上写了一个程序测试一下,果然能,这样就可以在调用ajax之前,先把数据通过javascript写到cookie里,然后再send就可以将cookie里的数据发送出去了,太爽了!!  

演示地址:http://cn5.cn/ajax/ajax12.htm  

客户端代码 ajax.htm  

<title>AJAX使用cookie传值例子</title>   
<script>   
var oDiv          
var xh                 
function getXML()   
{   
    setcookie($(name).value,$(val).value);   
    oDiv = document.all.m   
    oDiv.innerHTML = "正在装载,请稍侯......."   
    oDiv.style.display= ""   
    xh = new ActiveXObject("Microsoft.XMLHTTP")   
    xh.onreadystatechange = getReady   
    xh.open("POST","a.php",false)   
    xh.send();   
}   

function getReady()   
{   
    if(xh.readyState==4)   
    {   
        if(xh.status==200)   
        {   
            oDiv.innerHTML = "完成"   
        }   
        else   
        {   
             oDiv.innerHTML = "抱歉,装载数据失败。原因:" + xh.statusText   
        }   
  }   
} //author : longbill      www.longbill.cn   

function setcookie(name,value)   
{   
    var cookiestr=name+"="+value+";";   
    var expires = "";   
    var cookieexp=60*60*1000;   
    var d = new Date();   
    d.setTime( d.getTime() + cookieexp);   
    expires = "expires=" + d.toGMTString()+";";   
    document.cookie = cookiestr+ expires;   
}   
function $(a)   
{   
    return document.getElementById(a);   
}   
</script>   

<body>   
AJAX使用cookie传值例子:<br>   
<form name=myform>   
name:<input id=name  value="变量名甚至可以是中文" size=20><br>   
value:<input type=text size=20 id=val value=这里><br>   
<input onclick="getXML()" type="button" value="送出数据">   
<input onclick="if(xh && xh.responseText) {alert(xh.responseText);}" type="button" value="显示返回结果"><br>   
<div id=m bgcolor=blue>在此显示状态</div>   
<input type=button onclick="alert(document.cookie)" value=显示本地COOKIE>   
</form> 

服务器端代码 a.php  

<?   
header("Content-type: text/html;charset=GB2312");   
echo "以下是您送出的所有COOKIE变量及其值\n";   
print_r($_COOKIE);   
?>

 

 

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400009681&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao12345%2Fp%2F3725833.html&dt=1400009681957&shv=r20140508&cbv=r20140417&saldr=sb&correlator=1400009681732&frm=20&ga_vid=1003740855.1399798249&ga_sid=1399990330&ga_hid=1600630093&ga_fc=1&u_tz=480&u_his=780&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=0&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=W6zSKNtcBB&p=http%3A//www.cnblogs.com&dtd=47" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400009681&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao12345%2Fp%2F3725833.html&dt=1400009682011&shv=r20140508&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1400009681732&frm=20&ga_vid=1003740855.1399798249&ga_sid=1399990330&ga_hid=1600630093&ga_fc=1&u_tz=480&u_his=780&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=250&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=8vbSnphdCE&p=http%3A//www.cnblogs.com&dtd=44" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>