xml 筆記二 XMLHttpRequest

         一、      XMLHttpRequest 對象用於在後臺與服務器交換數據。

                         1、不從新加載網頁來更新網頁

                          2、頁面加載後向服務器請求數據

                          3、頁面加載後從服務器接收數據

                          4、頁面加載後向服務器發送數據

         二、   XMLHttpRequest 對象創建

                    1、新版本瀏覽器

                        xmlHttp=new XMLHttpRequest();

                   2、老版本 ie5、ie6

                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

                   3、兼容模式

                    <script type="text/javascript">

                      var xmlHttp;

                     if(window.XMLHttpRequest)

                       {

                         xmlHttp=new XMLHttpRequest();

                       }

                     else if(window.AxtiveXObject)

                      {

                          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

                      }

                    </script>

       三、   向服務器取回數據

              <script type="text/javascript">

             function loadXML()

             {

                  var xmlHttp;

                  if(window.XMLHttpRequest)

                   {

                        xmlHttp=new XMLHttpRequest();

                   }

                   else if (window.AciveXObject)

                   {

                       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

                   }

                   if(xmlHttp!=null)

                   {

                        //onreadystatechange 加載狀態變化事件 

                       xmlhttp.onreadystatechange=state_Change

                       xmlhttp.open("GET",url,true); 

                       xmlhttp.send(null);

                   }

             }

              function state_Change()

                {

                  if (xmlhttp.readyState==4) 

                  {

                          //加載xml

                    if (xmlhttp.status==200)   

                      {

                           //完成加載

                          //加載完xml後 我們可以在這進出xml數據處理

                       } 

                     else   

                       {   

                        alert("xml數據加載異常");   

                       } 

                      }

                   }

              </script>

 

發佈了51 篇原創文章 · 獲贊 58 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章