Use javascript with ajax to read txt file content

Source: Internet
Author: User

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 );
}
};
};
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.