Ajax淺談

來源:互聯網
上載者:User

標籤:alert   ready   microsoft   activex   重新整理   方法   http請求   url   end   

一.什麼是Ajax?

Ajax是無重新整理資料讀取。

二.Ajax的原理:

HTTP要求方法

1.GET—用於擷取資料(如:瀏覽文章)

2.POST—用於上傳資料(如:使用者註冊)

三.Ajax請求的編寫:

Ajax請求的思路

1.建立Ajax對象

//IE6以上

var oAjax=new  XMLHttpRequest();

//IE6

var oAjax=new ActiveXObject(“Microsoft.XMLHTTP”);

//解決IE6的相容性問題

var oAjax=null;

if(window.XMLHttpRequest)
{
oAjax=new XMLHttpRequest();
}
else
{
oAjax=new ActiveXObject(“Microsoft.XMLHTTP”);
}

2.連結的伺服器

//open(方法, url, 是否非同步)
oAjax.open(‘GET’, ‘abc.txt’, true);//其中abc.txt是路徑

3.發送請求

oAjax.send();

4.接收返回

oAjax.onreadystatechange=function ()
{
if(oAjax.readyState==4)
{
if(oAjax.status==200)
{
alert(‘成功:’+oAjax.responseText);
}
else
{
alert(‘失敗’);
}
}
};
};

其中:

請求狀態監控

onreadystatechange事件

readyState屬性:請求狀態

>0(未初始化)還沒有調用open()方法

>1 (載入)已調用send()方法,正在發送請求

>2 (載入完成)send()方法完成,已收到全部響應內容

>3 (解析)正在解析響應內容

>4 (完成)響應內容解析完成,可以在用戶端調用了 status屬性:請求結果 responseText

status屬性:請求結果

responseText

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.