Use javascript with ajax to read txt file content
The code is concise, so there is not much nonsense here. Go to the source code directly.
Html code
The Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
</Head>
<Body>
<Button type = "button" onclick = "show ()"> request data </button>
<Script src = "ajax. js"> </script>
<Script>
Function show (){
Ajax('read.txt? Datetime = new Date. gettime', function (str) {alert (str) ;}, function () {alert ('failed ');})
};
</Script>
</Body>
</Html>
Javascript code
The Code is as follows:
Function Ajax (url, fnSucc, fnFaild)
{
// 1. Create an 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 the response
OAjax. onreadystatechange = function ()
{
If (oAjax. readyState = 4)
{
If (oAjax. status = 200)
{
FnSucc (oAjax. responseText );
}
Else
{
FnFaild (oAjax. status );
}
};
};
}