Ajax非同步呼叫架構

來源:互聯網
上載者:User
ajax|非同步 /* 
 作者:welfred 
 時間:05年8月14日  
 作用:Ajax非同步呼叫架構 
*/ 
var req; 

// retrieve XML document (reusable generic function); 
// parameter is URL string (relative or complete) to 
// an .xml file whose Content-Type is a valid XML 
// type, such as text/xml; XML source must be from 
// same domain as HTML file 
function ajax(url){ 
  
 req = false; 
  
    // branch for native XMLHttpRequest object 
    if(window.XMLHttpRequest) { 
     try { 
   req = new XMLHttpRequest(); 
        } catch(e) { 
   req = false; 
        } 
    // branch for IE/Windows ActiveX version 
    } else if(window.ActiveXObject) { 
        try { 
         req = new ActiveXObject("Msxml2.XMLHTTP"); 
       } catch(e) { 
         try { 
            req = new ActiveXObject("Microsoft.XMLHTTP"); 
         } catch(e) { 
            req = false; 
         } 
  } 
    } 
 if(req) { 
  req.onreadystatechange = processReqChange;  //Once the state changed,the function "processReqChange" will be excuted. 
  req.open("GET", url, true); 
  req.send(); 
 } 


/*Wait for the response*/ 
function processReqChange()  

    // only if req shows "complete" 
    if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 
   //Here is  your bussiness code 
   procBusiness(); 
        } else { 
            alert("獲得資料遇到問題!\n請保證您的網路串連暢通;否則,請及時與管理員聯絡,謝謝!:\n" + req.statusText); 
        } 
    } 


/* 
  Read the XML file and retrive the data */ 
function procBusiness(){ 
  var xmlResult = req.responseXML; 
  var root = xmlResult.documentElement; 

 //TO DO 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.