function createRequest(){
      if(window.XMLHttpRequest){
      httpRequest=new XMLHttpRequest();
      }else if(window.ActiveXObject){
      try{
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e){
      try{
       httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch(e){}
      }
      }
   }function sendRequest(url,nam1,nam2){
   createRequest();
   id1=nam1
   id2=nam2
   httpRequest.open("GET",url,true);
   httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=GB2312');   httpRequest.onreadystatechange=processResponse;
   httpRequest.send(null);
   } function processResponse(){
   if(httpRequest.readyState == 4){
   if(httpRequest.status == 200){
   //alert('200');
var result=httpRequest.responseText;
   //var result=httpRequest.responseXML//.getElementsByTagName("div");
   alert(result);   //var area=document.getElementById(id2);
   //area.innerHTML='<textarea rows=7 cols=49>'+result.value+'</textarea>'
   }
   }
   }发送数据的页面编码是GB2312。 header('Content-type: text/html;charset=GB2312');
$canshu=$_REQUEST["canshu"];
$shuliang=$_REQUEST["shuliang"];
$arr1=explode(":-",$canshu);
$arr2=explode(":-",$shuliang); echo $arr1[9];我发送的数据接收我拆分成数组,$arr1[9]里面包含中文和符号,乱码了!求解决方法,在线等

解决方案 »

  1.   

    由于XMLHTTP总是以utf-8传递数据,而不管提供给他的编码是什么
    所以如果页面编码是GB2312是需要用 encodeURIComponent函数将传递的数据转换成utf-8编码的URL串,在服务器端再用 iconv 转换成所需编码
      

  2.   

    encodeURIComponent函数将传递的数据转换成utf-8
    如果要改的话是从服务端转化
      

  3.   

    header('Content-type: text/html;charset=utf-8');
    ....
    echo iconv('gb2312','utf-8',$arr1[9]);
      

  4.   

    直接以utf-8编码更方便些,gb2312编码能显示的中文字符数太少了,很容易出现要显示的字符在字库里没有因而出现乱码的情况,不建议适用gb2312编码。