代码很简洁,这里就不多废话了,直接上源码

html代码

复制代码 代码如下:
 <!doctype html>
 <html>
  <head>
      <meta charset="utf-8"/>
      </head>
      <body>
          <button type="button"  onclick="show()">请求数据</button>
         <script src="/UploadFiles/2021-04-02/ajax.js">          <script>
           function show(){
             Ajax('read.txt"codetitle">复制代码 代码如下:
  function Ajax(url,fnSucc,fnFaild)
             {
                  //1.创建ajax对象
                if(window.XMLHttpRequest)
                 {// code for IE7+, Firefox, Chrome, Opera, Safari
                      var oAjax=new XMLHttpRequest();
                 }
                else
                 {// code for IE6, IE5
                     var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
                 }
                 //2.链接服务器(打开服务器的连接)
                 //open(方法,文件名,异步传输)
                 oAjax.open('GET',url,true);
                 //3.发送
                 oAjax.send();
                 //4.接收返回
                 oAjax.onreadystatechange=function()
                    {
                      if (oAjax.readyState==4)
                      {
                        if(oAjax.status==200)
                          {
                            fnSucc(oAjax.responseText); 
                          }
                       else
                          {
                           fnFaild(oAjax.status);
                          }
                      };
                 };
              }

请求的文件为read.txt

内容随便填写

标签:
javascript,ajax

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“javascript结合ajax读取txt文件内容”

暂无“javascript结合ajax读取txt文件内容”评论...