使用ajaxFunction("http://www.sina.com/")
以下代码调用为何返回undefine//ajax 获取
function ajaxFunction(url)
 {
 var xmlHttp;
 
 try
{
    xmlHttp=new XMLHttpRequest();   // Firefox, Opera 8.0+, Safari
}
 catch (e)
{
  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
}
    
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function()
  {
      try
      {
      if(xmlHttp.readyState==4)
        {
           return xmlHttp.responseText;//返回值
        }
      }
      catch(e)
      {}
  }
 }
而以下方法调用则可以执行,请问这是为什么//ajax 获取
function ajaxFunction(url)
 {
 var xmlHttp;
 
 try
{
    xmlHttp=new XMLHttpRequest();   // Firefox, Opera 8.0+, Safari
}
 catch (e)
{
  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
}
    
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function()
  {
      try
      {
      if(xmlHttp.readyState==4)
        {
           alert(xmlHttp.responseText);//返回值
        }
      }
      catch(e)
      {}
  }
 }

解决方案 »

  1.   

    你这里是这个 xmlHttp.onreadystatechange=function() 函数的返回值ajaxFunction 这个函数没有.用回调吧.function ajaxFunction(url, callback){
        ...
        callback(xmlHttp.responseText);
        ...
    }
      

  2.   


    第一个是:           return xmlHttp.responseText;//返回值第二个是:           alert(xmlHttp.responseText);//返回值--!
      

  3.   

    return xmlHttp.responseText的返回值给谁了???
    貌似给了onreadystatechange