JavaScript combines Ajax to read TXT file content _javascript tips

Source: Internet
Author: User
Tags button type

The code is very concise, here is not much nonsense, directly on the source

HTML code

Copy Code code as follows:

<!doctype html>
<meta charset= "Utf-8"/>
<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>

JavaScript code

Copy Code code as follows:

function Ajax (url,fnsucc,fnfaild)
{
1. Creating Ajax Objects
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. Linked server (open server connection)
Open (method, filename, 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)
{
FNSUCC (Oajax.responsetext);
}
Else
{
Fnfaild (Oajax.status);
}
};
};
}

The requested file is Read.txt

Fill in the content casually

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.