使用ajax技术实现txt弹出在页面上

使用ajax技术实现txt弹出在页面上

 

使用ajax技术实现点击按钮,将TXT文本里的内容通过弹出框显示到页面上

/*事件会在页面加载完成后触发。*/

<script>

window.onload = function(){

/*获取按钮的id*/

  var oBth=document.getElementById(‘btn’);

/*点击按钮触发的函数*//

  oBth.onclick = function(){

/*打开浏览器*/

    var xhr = new XMLHttpRequest();

/*在地址栏输入地址,这里的1txt代表需要打开的内个txt文件*/

    xhr.open(‘get’,‘1.txt’,true)

/*提交*/

    xhr.send();

/*等服务器返回内容*/

    xhr.onreadystatechange = function(){

      if(xhr.readystate==4){

      alert(xhr.responseText);

}

}

}

}

</script>

<body>

<input type="button" value="按钮" id="btn">

</body>

原文地址:https://www.cnblogs.com/520lin/p/5892945.html