漫談Ajax在.Net中的使用

來源:互聯網
上載者:User
ajax

AJAX出來的時間也不短了。雖然它在某些方面很受爭議,但是瑕不掩瑜。AJAX不能說它屬於哪門語言,而是任何一門語言與javascript,XML的交叉。我覺得,說Ajax是任何一門WEB語言與DHTML、XML的交叉這樣更加合適。

以下只討論IE部分。

Ajax在應用中使用有3個部分(個人觀點):
1、資料(一般通過IE內建群組件Microsoft.XMLHTTP來取得或者發送資料);
2、事件(事件指的是用戶端事件,如果是服務端事件,那麼AJAX也就沒什麼意義了);
3、綁定(暫且就叫綁定吧,也可以說是顯示,一般通過DHTML來完成)。

從上面看,Ajax就使用了Microsoft.XMLHTTP組件和DHTL。其實還有另外一部分,就是伺服器端的處理。

一、簡單樣本
就最簡單的原型來說,就是取得資料:
a.aspx的內容如下:
aaaaab.aspx取得
<div id="MyShow"/>

<script language="JavaScript">
    var xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    //資料轉送,flase為非非同步方式
    xmlhttp.open("GET","a.aspx",true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
        MyShow.InnerText = xmlhttp.responseText;
   }
   if (xmlhttp.readyState==3) {
        MyShow.InnerText  = ('正在提交資料');
   }
  }
    xmlhttp.send(null);
}
</script>
a.aspx提供可資料
xmlhttp.open("GET","a.aspx",true);就是請求a.aspx


    if (xmlhttp.readyState==4) {
        MyShow.InnerText = xmlhttp.responseText;
   }
當非同步請求完成時,用DHML改變MyShow的內容。

二、GET方法
更改a.aspx如下:
<script runat="Server" language="C#">
string flag = Request["flag"] == null ? "" : Request["flag"];
switch(flag)
{
    case "1":
        Response.Write("11111111111111");
        break;
    case "2" :
        Response.Write("22222222222222");
        break;
}
</script>
把b.aspx中
xmlhttp.open("GET","a.aspx",true);改成xmlhttp.open("GET","a.aspx?flag=1",true);
則得到資料11111111111111
xmlhttp.open("GET","a.aspx",true);改成xmlhttp.open("GET","a.aspx?flag=2",true);
則得到資料22222222222222

三、POST方法
如果有這樣一個表單
<form method=post>
    <input name="p1" type=text />
<input name="p2" type=submit/>
</form>
用AJAX就是
<div id="MyShow"/>

<script language="JavaScript">
    var xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    //資料轉送,flase為非非同步方式
    xmlhttp.open("Post","a.aspx",true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
        MyShow.InnerText = xmlhttp.responseText;
   }
   if (xmlhttp.readyState==3) {
        MyShow.InnerText  = ('正在提交資料');
   }
  }
       xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

    xmlhttp.send("p1=qwdqwdqwdqwd"); //這裡是POST要提交的資料。
}
</script>

而一般類比請求都是POST和Get同時存在的
只要把
 xmlhttp.open("Post","a.aspx",true);
裡a.aspx加上get請求部分就可以了。

而在.Net中特別得,可以把Ajax寫成伺服器組件來使用。現在在實際項目中如果使用Ajax很多的情況,就有個專門的組件來使用了。還有就是要注意,在很多時候Ajax的時候要設定頁面不緩衝。而如果要相容非IE核心瀏覽器,那麼就要注意各種核心瀏覽器的JS是否相容了。

http://birdshover.cnblogs.com/archive/2006/07/03/441439.html



相關文章

聯繫我們

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