xmlhttp的编码是utf8,你页面的编码最好为utf8,
如果不是,那么返回的字串要指定编码
php中用
header("Content-Type:text/html;charset=GB2312");另外不需要使用escape
使用encodeURIComponent("中文");就可以了,服务器端会自动解码
接收时也不必用unescape

解决方案 »

  1.   

    PHP页面是别人的,无法控制。。
    查了很多资料说要用escape一下,
    再用unescape回来就不会乱了,我的还是会乱。。有没有可以直接在我上面贴出的代码里控制编码的?
      

  2.   

    我只要得到对方页面,并正确显示就OK了。
    找到了一个JS的编码转换,好像没用。你们都是怎么整的这个?
      

  3.   

    不好意思,自己转到JS版来了。
    大家看看得到的页面信息编码是utf-8的,显示为乱码,能否用js来改变其编码方式,不会显示乱码?
      

  4.   

    你看看这个帖子http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329看他是怎么处理中文乱码的。还有最后一个回复,对处理中文问题的一个改进,值得一看
      

  5.   

    当然有时候,为了达到同一个目的。要想办法避开中文问题。中文字符。比如这个帖子
    http://community.csdn.net/Expert/topic/4751/4751359.xml?temp=.3371546
    相对于
    http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329巧妙避开了对中文问题的处理,改进后程序速度效率提高了几个数量级
      

  6.   

    var html1 = unescape(request.responseText);//这里输出是乱码
    为什么要unescape?
    不能unescape,你可以用vbs将gbk转为utf8.由于服务器是别人的,所以你只能这么做。
      

  7.   

    http://community.csdn.net/Expert/topic/4748/4748452.xml?temp=.6224329这个帖子得到的难道不是中文,你alert出来看看
      

  8.   

    unescape是我在网上查的,说加上去不会乱码,但我加了还是乱,我去掉也乱。
    request.responseText回来的不是utf-8的编码吗??我想有没有用js转换utf-8成gb2312的。Go_rush,如果我没有看错的话,你是取得中文部分再来转换的。
    我这个是整个页面啊,是不是不同?
    我直接COPY那个VBS的函数和reCode()来用,会出错,不知道有什么要求?
    Javascrip基础比较差,如果能讲解一下就太感谢了。
      

  9.   

    1.不需要escape和unescape。
    2.如果服务器你不能修改代码,那就需要进行编码转换,这个只能用VBS做到
    网上现在的代码
    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>
      

  10.   

    to ice_berg16(寻梦的稻草人)用數組,不要用那個字符串Function bytes2BSTR(vIn)
        Dim strReturn()
    j=LenB(vIn)
    ReDim strReturn(j+1)
        For i = 1 To j
    ThisCharCode = AscB(MidB(vIn,i,1))
            If ThisCharCode < &H80 Then
                strReturn(i) = Chr(ThisCharCode)
            Else
    NextCharCode = AscB(MidB(vIn,i+1,1))
                strReturn(i) = Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
                i = i + 1
            End If
        Next
        bytes2BSTR = Join(strReturn,"")
    End Function
      

  11.   

    go_rush不好意思,之前我没有用上,是因为responseText,现在我改用了responseBody
      

  12.   

    最后成这样子就对了。哈哈。。多谢楼上的每一位。。
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD><BODY>
    <script language="javascript" type="text/javascript">   var request = false;
       try {
         request = new XMLHttpRequest();
       } catch (trymicrosoft) {
         try {
           request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (othermicrosoft) {
           try {
             request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (failed) {
             request = false;
           }  
         }
       }   if (!request)
         alert("Error initializing XMLHttpRequest!");   function getCustomerInfo() {
         var phone = document.getElementById("phone").value;
         var url = "http://192.168.1.223/ajax/test.php?phone=" + escape(phone);
         request.open("GET", url, true);
         request.onreadystatechange = updatePage;
         request.send(null);
       }
       function getSalesData() {
         var url = "http://192.168.1.223/ajax/test.php";
         request.open("HEAD", url, true);
         request.onreadystatechange = updatePage;
         request.send(null);
       }
       function postCustomerInfo() {
         var key = document.getElementById("key").value;
         var submit1 = document.getElementById("submit1").value;
         var page = document.getElementById("page").value;
         var url = "http://www.0577hr.com/search_.php";
     var postUrl = "key=" + key + "&submit1=" + submit1 +"&page=" + page;     request.open("POST", url, true);
     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         request.onreadystatechange = updatePage;
         request.send(postUrl);
       }   function updatePage() {
         if (request.readyState == 4){
           //alert(request.responseText);
       var html1 = bytes2BSTR(request.responseBody);
       html.document.open();
       html.document.write( html1 );
       html.document.close();
     }
       }   function loading(){
     html.document.open();
         html.document.write( "Loading..." );
     html.document.close();
       }
    </script><table border=0 cellspacing=1 cellpadding=1 bgcolo=eeeeee>
      <tr>
        <form name="pagesee" method="post">
        <input type="hidden" id="key" value="where pubdate >= NOW() - INTERVAL 3 DAY  && i_state != 2 && c_state !=2 ">
        <input type="hidden" id="submit1" value="">
        <td colspan=5 align=right><input type="text" id="page" value="1" size=5>
    <input type=button onclick='loading();page.value--;postCustomerInfo();' value='PREV'> | 
    <input type=button onclick='loading();page.value++;postCustomerInfo();' value='NEXT'>
        </td>
        </form>
      </tr>
    </table>
    <iframe id="html" name="html" src="" width="100%" height="400"></iframe>
    <script language="vbs">
    Function bytes2BSTR(vIn)
        Dim strReturn()
    j=LenB(vIn)
    ReDim strReturn(j+1)
        For i = 1 To j
    ThisCharCode = AscB(MidB(vIn,i,1))
            If ThisCharCode < &H80 Then
                strReturn(i) = Chr(ThisCharCode)
            Else
    NextCharCode = AscB(MidB(vIn,i+1,1))
                strReturn(i) = Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
                i = i + 1
            End If
        Next
        bytes2BSTR = Join(strReturn,"")
    End Function</script>
    </BODY>
    </HTML>