Ajax核心技術代碼分享_AJAX相關

來源:互聯網
上載者:User

複製代碼 代碼如下:

<script>
    var xhr = '';
    function Ajax()
    {
        if(window.XMLHttpRequest)
        {
                var xhr = new XMLHttpRequest;//現代瀏覽器
        }else
        {
                var xhr = new ActiveXObject('Microsoft.XMLHTTP');//IE
        }
    }
    Ajax();//1.擷取Ajax對象

    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4 && xhr.status == 200)
        {
            var data = xhr.responseText;//2.判斷狀態,接收資料(字串形式),後續操作
        }
    }

    //xhr.open('請求方式', '請求地址', TRUE);//3.非同步傳輸串連
    //xhr.send([data]);//4.發送請求

    xhr.open('GET', 'demo.php?name=weichen&sex=0', TRUE);
    xhr.send();

    /*
    xhr.open('POST', 'demo.php', TRUE);
    xhr.setResponseHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send('name=weichen&sex=0'); 
    */

XHR對象中的成員屬性和成員方法

複製代碼 代碼如下:

{

成員屬性:
      status           伺服器響應的HTTP狀態代碼(如200或404)
      statusText        伺服器響應的以字串形式返回的HTTP狀態代碼(ok或not found)
      readyState        瀏覽器請求的狀態代碼(0到4)
      responseText         伺服器響應的字串格式的資料結果
      responseXML      伺服器響應的XML格式的資料結果
      onreadystatechange   Ajax的請求事件(當readystate改變時調用的事件處理函數)
      onerror          請求過程中發生錯誤時調用的Mozilla的事件處理函數
      onprogress        內容載入後調用的mozilla的事件處理函數
      onload           文檔載入完畢後調用的mozilla的事件處理函數
成員方法:
      setRequestHeader();        設定當前請求的header頭資訊(為即將發送到伺服器端的訊息頭增加一個鍵/值對)
      open();               建立一個新的請求串連(用GET、POST、URL等初始化XHR對象)
      send();               發送一個請求(可能需要發送的資料)
      getResponseHeader("server");   返回指定的HTTP頭的值(如server或last-modified)
      getAllResponseHeaser();      返回由分行符號分割開的所有HTTP頭的字串
      absort();              終止請求

}

聯繫我們

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