JavaScript reads TXT file content with Ajax

Source: Internet
Author: User
Tags button type

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

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


                          }


                      };


                 };


              }
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.