解决xmlhttp乱码的方法

我最近也在研究xmlhttp乱码 的解决办法,我测试了一下,下面是测试代码,如果能把escape转换后的编码再转换成中文,输出的时候反操作就解决问题了。

<SCRIPT language=JavaScript>
function AddDataPost(sUserId,sUserName)
{
   var oBao = new ActiveXObject("Microsoft.XMLHTTP");
sUserId = escape(sUserId);
   sUserName = escape(sUserName);
   var userInfo = "id="+sUserId+"&name="+sUserName;
   oBao.open("POST"," http ://www.ixinu.com/0/Server.asp",false);
   oBao.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=gb2312")
   oBao.send(userInfo);
   delete(oBao);
   document.getElementById("data").innerHTML = unescape(oBao.responseText);
}
</SCRIPT>

<INPUT onclick=AddDataPost(document.all.id.value,document.all.name.value) type=button value=submit>

userid:<INPUT name=id>

username:<INPUT name=name>
<SPAN id=data></SPAN>
<SCRIPT>document.write(""+escape("我")+"");</SCRIPT>
<SCRIPT>document.write(""+unescape("%u6211")+"");</SCRIPT>

刚才的那个结果测试已经通过了,还有一个更简单的方法,原来:escape和unescape在JS和ASP里面可以通用,所以直接在输出反编码就得到了中文的输出,而不需要经常其它编码操作了,呵呵!

客户端代码:

<SCRIPT language=JavaScript>
function AddDataPost(sUserId,sUserName)
{
   var oBao = new ActiveXObject("Microsoft.XMLHTTP");
   sUserId = escape(sUserId);
   sUserName = escape(sUserName);
   var userInfo = "id="+sUserId+"&name="+sUserName;
   oBao.open("POST","Server.asp",false);
   oBao.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=gb2312")
   oBao.send(userInfo);
   delete(oBao);
   document.getElementById("data").innerHTML = unescape(oBao.responseText);
}
</SCRIPT>

<INPUT onclick=AddDataPost(document.all.id.value,document.all.name.value) type=button value=submit> userid:<INPUT name=id>
username:<INPUT name=name>
<SPAN id=data></SPAN>
<SCRIPT>document.write(""+escape("我")+"");</SCRIPT>
<SCRIPT>document.write(""+unescape("%u6211")+"");</SCRIPT>


服务器端代码:Server.asp <% Response.Write "<br />"&escape(Request.Form("name"))&vbcrlf  %>

posted @ 2010-04-21 10:46  y0umer  阅读(354)  评论(0编辑  收藏  举报