<body>
<input type="button" id="btn" value="按钮"/>
<script>
var oBtn=document.getElementById("btn");
oBtn .οnclick=function(){
//打开浏览器
/** var xhr=null;
if(window.XMLHttpRequest){//window.XMLHttpRequest window下的MLHttpRequest若不存在返回的是undefined,
// 若只写MLHttpRequest,不存在则返回的是false
xhr=new XMLHttpRequest();
}
else{
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}*/
//另一种兼容的方式
try{//代码尝试在地址栏输入执行这个块中的内容,如果有错误,则会执行catch{},并且传入错误信息参数
xhr=new XMLHttpRequest();
}
catch (e){
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
xhr.open('get','1.txt','true');
//提交
xhr.send();
//等待服务器返回内容
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
alert(xhr.responseText);
}
}
}
</script>
</body>
ajax兼容问题代码
最新推荐文章于 2021-08-05 14:57:35 发布