<script>var apiname;
function handleStateChange()
{
    if(xmlHttp.readyState == 4){
     if(xmlHttp.status == 200){
           result = xmlHttp.responseText;
           //alert("The server replied with:" + result);
          apiname=(result.substring(result.indexOf("<title>")+7,result.indexOf("</title>")));
          alert(apiname);
        }
        else{
           //alert(xmlHttp.status);
        }        
     }
    else{
         window.status = xmlHttp.readyState;
       }
}
function startRequest(s)
{
     if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequest)
  {
      xmlHttp = new XMLHttpRequest();
  }
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET",s, true);
    xmlHttp.send(null);
    //return ??
    //怎样返回handleStateChange()  alert的内容?
}
var a;
a = startRequest("http://www.baidu.com");
</script>请问怎样返回handleStateChange() 里面 alert的内容?想把网页源码赋值给变量a,不会写
如果在handleStateChange()里面return apiname,startRequest()得到的是undefined,

解决方案 »

  1.   

    感觉你没有必要去得到它的返回值吧。你可以把你要的操作放到回调方法中去呀。如果你一定要返回值的话,那你就不要用异步了,你用同步得了。
     xmlHttp.open("GET",s, true);
    把这个改为:
     xmlHttp.open("GET",s, false);
    这样,可以得到值。
      

  2.   

    感觉你没有必要去得到它的返回值吧。你可以把你要的操作放到回调方法中去呀。如果你一定要返回值的话,那你就不要用异步了,你用同步得了。
     xmlHttp.open("GET",s, true);
    把这个改为:
     xmlHttp.open("GET",s, false);
    这样,可以得到值。
      

  3.   


    <script>var apiname;
    function handleStateChange()
    {
        if(xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){
               result = xmlHttp.responseText;
               //alert("The server replied with:" + result);
              apiname=(result.substring(result.indexOf("<title>")+7,result.indexOf("</title>")));
              //alert(apiname);
            }
            else{
               //alert(xmlHttp.status);
            }        
         }
        else{
             window.status = xmlHttp.readyState;
           }
    }
    function startRequest(s)
    {    
         if (window.ActiveXObject)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if(window.XMLHttpRequest)
      {
          xmlHttp = new XMLHttpRequest();
      }
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open("GET",s, false);
        xmlHttp.send(null);
        //return ??
        //怎样返回handleStateChange()  alert的内容?
    }
    var a;
    a = startRequest("http://www.baidu.com");
    alert(a);
    </script>改成false了,可alert(a);还是undefined
      

  4.   

    你不是定义了一个全局变量var apiname;
    等到服务器返回数据赋给apiname的时候你就可以直接赋给变量a
    其实这个时候你已经抓取了返回数据,不用再定义一个a来保存结果了。
      

  5.   

    <script>var apiname;
    function handleStateChange()
    {
        if(xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){
               result = xmlHttp.responseText;
               //alert("The server replied with:" + result);
              apiname=(result.substring(result.indexOf("<title>")+7,result.indexOf("</title>")));
              alert(apiname);
            }
            else{
               //alert(xmlHttp.status);
            }        
         }
        else{
             window.status = xmlHttp.readyState;
           }
    }
    var xmlHttp;//全局变量
    function startRequest(s)
    {    
         if (window.ActiveXObject)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if(window.XMLHttpRequest)
      {
          xmlHttp = new XMLHttpRequest();
      }
        xmlHttp.open("GET",s, true);//这里的true/false是异步、同步设置
        xmlHttp.onreadystatechange = handleStateChange;
        
        xmlHttp.send(null);
        //return ??
        //怎样返回handleStateChange()  alert的内容?
    }
    var a;
    a = startRequest("http://www.baidu.com");
    </script>
      

  6.   

    <script>var apiname;
    var xmlHttp;//全局变量
    function startRequest(s)
    {    
         if (window.ActiveXObject)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if(window.XMLHttpRequest)
      {
          xmlHttp = new XMLHttpRequest();
      }
        xmlHttp.open("GET",s, true);//这里的true/false是异步、同步设置
        xmlHttp.onreadystatechange = handleStateChange;
        
        xmlHttp.send(null);
        //return ??
        //怎样返回handleStateChange()  alert的内容?
    }//换个位置,不然xmlHttp可能会是undefined
    function handleStateChange()
    {
        if(xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){
               result = xmlHttp.responseText;
               //alert("The server replied with:" + result);
              apiname=(result.substring(result.indexOf("<title>")+7,result.indexOf("</title>")));
              alert(apiname);
            }
            else{
               //alert(xmlHttp.status);
            }        
         }
        else{
             window.status = xmlHttp.readyState;
           }
    }var a;
    a = startRequest("http://www.baidu.com");
    </script>