Home  >  Article  >  Web Front-end  >  Javascript combined with ajax to read the content of txt file_javascript skills

Javascript combined with ajax to read the content of txt file_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:28:561749browse

The code is very concise, so there will be no more nonsense here, just go to the source code

html code

Copy code The code is as follows:




         


                                                                                                                                                                                                                       .                                      
                                                                                                    function show(){
Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},function(){alert('failed');})
            };
                                                                                         




javascript code

Copy code The code is as follows:

function Ajax(url,fnSucc,fnFaild)
                 {
//1. Create ajax object
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. Link to the server (open the connection to the server)
//open(method, file name, asynchronous transmission)
                   oAjax.open('GET',url,true);
//3.Send
                  oAjax.send();
//4.Receive return
oAjax.onreadystatechange=function()
                                                       {
If (oAjax.readyState==4)
                                                               {
If(oAjax.status==200)
                                                                                                                                                                                                                                                                                                             through fnSucc(oAjax.responseText);
                                                                                                       }                                                      else                                                                                                                                                                                                                                                                                                    through fnFaild(oAjax.status);
                                                                                                       }                        };
                  };
              }



The requested file is read.txt

Fill in the content as you like
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn