AJAX基本格式步驟

來源:互聯網
上載者:User

標籤:send   請求   div   url   ready   col   null   狀態   hang   

第一步:建立XMLHttpRequest對象var xmlhttp;//相容性if (window.XMLHttpRequest)  {  xmlhttp=new XMLHttpRequest();  }else  {  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }代碼解釋:
  1. 首先建立一個作為 XMLHttpRequest 對象使用的 XMLHttp 變數。把它的值設定為 null。
  2. 然後測試 window.XMLHttpRequest 對象是否可用。在新版本的 Firefox, Mozilla, Opera 以及 Safari 瀏覽器中,該對象是可用的。
  3. 如果可用,則用它建立一個新對象:XMLHttp=new XMLHttpRequest()
  4. 如果不可用,則檢測 window.ActiveXObject 是否可用。在 Internet Explorer version 5.5 及更高的版本中,該對象是可用的。
  5. 如果可用,使用它來建立一個新對象:XMLHttp=new ActiveXObject()
 第二步:確定來自伺服器的響應是否XML  ,  async的值 1.如果來自伺服器的響應並非 XML,請使用 responseText 屬性 ··當使用 async=true 時,請規定在響應處於 onreadystatechange 事件中的就緒狀態時執行的函數:xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }   ··當您使用 async=false 時,請不要編寫 onreadystatechange 函數 - 把代碼放到 send() 語句後面即可:document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 2.如果來自伺服器的響應是 XML,而且需要作為 XML 對象進行解析,請使用 responseXML 屬性: xmlDoc=xmlhttp.responseXML;txt="";x=xmlDoc.getElementsByTagName("標記");for (i=0;i<x.length;i++)  {  txt=txt + x[i].childNodes[0].nodeValue + "<br />";  }document.getElementById("myDiv").innerHTML=txt; 第三步:向伺服器發送請求:xmlhttp.open("GET","test1.txt",true);xmlhttp.send(); xmlhttp.open("POST","ajax_test.asp",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//要求標頭應在open和send之間xmlhttp.send("fname=Bill&lname=Gates");

AJAX基本格式步驟

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.