<script>function startRequest() {var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "1.txt", true);
xmlhttp.send(null);
alert(xmlhttp.responseText);
}
</script>
<input name="" type="button" onclick="startRequest();" value="请求服务器" />页面刚打开的时候,经常出现的情况是,点击按钮,不会跳出 alert窗口,得再点一次按钮。但有时又很正常。
有时还会出现错误信息:

解决方案 »

  1.   

    你这是同步的写法,所以这里要:
    xmlhttp.open("GET", "1.txt", false);如果是异步(xmlhttp.open("GET", "1.txt", true);)
    要这样:var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange=function(){
      alert(xmlhttp.responseText);
    }

    xmlhttp.open("GET", "1.txt", true);
    xmlhttp.send(null);
      

  2.   

    <script>function startRequest() {var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET", "1.txt", true);
    xmlhttp.send(null);
    xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState == 4){
      if(xmlhttp.status == 200){
        alert(xmlhttp.responseText);
      }
     }
    };
    }
    </script>
    <input name="" type="button" onclick="startRequest();" value="请求服务器" />楼主试试!你写的那个数据还没有得到你就想把它alert出来,当然要报错啦!!
      

  3.   

    谢谢大家,我把代码里的ture改成false,就正常了
    第一次ajax!
    如果我改成false了,是不是就不需要使用 onreadystatechange 呢?
      

  4.   

    楼主Ajax的基本知识还是很缺乏的,建议楼主补补,会好点。