DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
AJAX 核心三部曲:建立、請求與回呼
編輯:AJAX詳解     

AJAX 的核心可以拆成三步:建立 XMLHttpRequest 物件、送出請求、處理伺服器回應。只要掌握這三步,就能做出基本的非同步互動。

建立物件

function createXMLHttpRequest() {
  if (window.ActiveXObject) {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
  } else if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  }
}

送出請求

function checkName() {
  createXMLHttpRequest();
  xhr.onreadystatechange = handleStateChange;
  xhr.open("GET", "UserReg.aspx?", true);
  xhr.send(null);
}

處理回應

function handleStateChange() {
  if (xhr.readyState == 4) {
    if (xhr.status == 200) {
      document.getElementById("divx").innerHTML = xhr.responseText;
    }
  }
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved