首页  >  文章  >  web前端  >  javascript结合ajax读取txt文件内容_javascript技巧

javascript结合ajax读取txt文件内容_javascript技巧

WBOY
WBOY原创
2016-05-16 16:28:561749浏览

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

html代码

复制代码 代码如下:

 
 
 
     
     
     
         
        
         <script><br /> function show(){<br /> Ajax('read.txt&#63;datetime=new Date.getTime ',function(str){alert(str);},function(){alert('失败了');})<br /> };<br /> </script>
     
 

javascript代码

复制代码 代码如下:

  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

内容随便填写

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn