文章出處


1
<script type="text/javascript"> 2 // var a=1; 3 // alert(window.a); 4 // a屬于window下的一個屬性 5 /*function show() 6 { 7 alert('a') 8 }; 9 window.show()*/ 10 //alert(a)----會出錯和alert(window.a)----結果是undefined;的區別 11 </script>
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>無標題文檔</title>
 6 <script>
 7 window.onload=function ()
 8 {
 9     var oBtn=document.getElementById('btn1');
10     
11     oBtn.onclick=function ()
12     {
13         //1.創建ajax對象
14         //IE6以上
15         /*var oAjax=new XMLHttpRequest();
16         
17         alert(oAjax);*/
18         
19         //IE6
20         /*var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
21         
22         alert(oAjax);*/
23         var oAjax=null;
24         
25         if(window.XMLHttpRequest)
26         {
27             oAjax=new XMLHttpRequest();//兼容火狐,谷歌瀏覽器
28         }
29         else
30         {
31             oAjax=new ActiveXObject("Microsoft.XMLHTTP");//IE自帶的控件
32         }
33         
34         //2.連接服務器
35         //open(方法, url, 是否異步)get和post方法的選擇需要和后臺需求來定;文件的地址;絕大多數都是異步傳輸,這樣可以同時做多件事情。
36         oAjax.open('GET', 'abc.txt', true);
37         
38         //3.發送請求
39         oAjax.send();
40         
41         //4.接收返回
42         //OnReadyStateChange
43         oAjax.onreadystatechange=function ()
44         {
45             if(oAjax.readyState==4)//4表示完成
46             {
47                 if(oAjax.status==200)//表示成功
48                 {
49                     alert('成功:'+oAjax.responseText);//返回文本內容
50                 }
51                 else
52                 {
53                     alert('失敗');
54                 }
55             }
56         };
57     };
58 };
59 </script>
60 </head>
61 
62 <body>
63 <input id="btn1" type="button" value="讀取文件" />
64 </body>
65 </html>

請求狀態監控 onreadystatechange事件

readyState屬性:

請求狀態

0 (未初始化)還沒有調用open()方法

1 (載入)已調用send()方法,正在發送請求

2 (載入完成)send()方法完成,已收到全部響應內容

3 (解析)正在解析響應內容

4 (完成)響應內容解析完成,可以在客戶端調用了

status屬性:請求結果。成功的話會返回

responseText返回文件的內容200,失敗會彈出404等數字。


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()