<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窗口,得再点一次按钮。但有时又很正常。
有时还会出现错误信息:
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);
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出来,当然要报错啦!!
第一次ajax!
如果我改成false了,是不是就不需要使用 onreadystatechange 呢?