1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 |
优点:使用ajax读取数据文件,不需要刷新页面就能取出文件数据<br><br><br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <head> <title></title> <script type= "text/javascript" > function
ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 if
(window.XMLHttpRequest) { var
oAjax = new
XMLHttpRequest(); } else
{ var
oAjax = new
ActiveXObject( "Microsoft.XMLHTTP" ); } //2.连接服务器 //open(方法, 文件名, 异步传输) oAjax.open( ‘GET‘ , url, true ); //3.发送请求 oAjax.send(); //4.接收返回 oAjax.onreadystatechange = function
() { //oAjax.readyState //浏览器和服务器,进行到哪一步了 if
(oAjax.readyState == 4) //读取完成 { if
(oAjax.status == 200) //成功 { fnSucc(oAjax.responseText); } else
{ if
(fnFaild) { fnFaild(oAjax.status); } //alert(‘失败:‘+oAjax.status); } } }; } window.onload = function
() { var
oBtn = document.getElementById( ‘myDiv‘ ); oBtn.onclick = function
() { ajax( ‘test1.txt?t=‘ + new
Date().getTime(), function
(str) { <br> //?t=‘+new Date().getTime() 可以控制缓存,即每次改变 test1.txt文件不需要刷新页面既可读取文件改变后的值 |
1 |
var
oTxt = document.getElementById( ‘txt‘ );<br> oTxt.value = str; }); }; }; <br></script> <br></head> <br><body><br> <input type= "button"
id= "myDiv"
value= "读取" /><br /> <br>用户名: <input type= "text"
id= "txt"
/><br> <input type= "text"
id= "Text1"
/> <br></body><br> </html> |
原文:http://www.cnblogs.com/izhiniao/p/3769994.html