bytes2BSTR 解决ajax中ajax.responseBody 乱码问题

Posted on 2010-07-11 13:24  黔人阿诺  阅读(1292)  评论(0编辑  收藏  举报
代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>XMLHttp</title>
<script language="javascript">
<!--
/**
* 初始化一个xmlhttp对象
*/
function InitAjax()
{
  
var ajax=false
  
try { 
     ajax 
= new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
  
catch (e) { 
          
try { 
                ajax 
= new ActiveXObject("Microsoft.XMLHTTP"); 
              }
         
catch (E) { 
                ajax 
= false
              } 
            }
    
if (!ajax && typeof XMLHttpRequest!='undefined') { 
    ajax 
= new XMLHttpRequest(); 
    } 
    
return ajax;
  } 
function show_total(newsID)
{
//如果没有把参数newsID传进来
if (typeof(newsID) == 'undefined')
{
return false;
}
//需要进行Ajax的URL地址
var url = "http://192.168.1.208/text.txt?random="+Math.random();
//获取新闻显示层的位置
var show = document.getElementById("show_total"); 
//实例化Ajax对象
var ajax = InitAjax();
//使用Get方式进行请求 
ajax.open("GET", url, true); 
//获取执行状态
ajax.onreadystatechange = function() { 
//如果执行是状态正常,那么就把返回的内容赋值给上面指定的层
if (ajax.readyState == 4 && ajax.status == 200) { 
show.innerHTML 
=  bytes2BSTR(ajax.responseBody); 


}
//发送空
ajax.send(null); 
}
//定时开始
var sec = 0;
timerID 
= setInterval("show_total(1)",1000);

 
//-->
</script>
<script type="text/vbscript">
Function bytes2BSTR(vIn)  
    strReturn 
= ""  
    For i 
= 1 To LenB(vIn)  
        ThisCharCode 
= AscB(MidB(vIn,i,1))  
        If ThisCharCode 
< &H80 Then  
            strReturn 
= strReturn & Chr(ThisCharCode)  
        Else  
            NextCharCode 
= AscB(MidB(vIn,i+1,1))  
            strReturn 
= strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))  
            i 
= i + 1  
        End If  
    Next  
    bytes2BSTR 
= strReturn  
End Function  
</script> 
</head>
<body>
实时快报:
<div id=show_total>
</div>
</body>
</html>