AJAX ASP 請求執行個體

來源:互聯網
上載者:User

AJAX 用於創造動態性更強的應用程式。

AJAX ASP 執行個體

下面的例子將為您示範當使用者在輸入框中鍵入字元時,網頁如何與 網頁伺服器進行通訊:

請在下面的輸入框中鍵入字母(A - Z):

姓氏:

建議:

==============

 

<html><head><script type="text/javascript">function showHint(str){var xmlhttp;if (str.length==0)  {   document.getElementById("txtHint").innerHTML="";  return;  }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","/ajax/gethint.asp?q="+str,true);xmlhttp.send();}</script></head><body><h3>請在下面的輸入框中鍵入字母(A - Z):</h3><form action=""> 姓氏:<input type="text" id="txt1" onkeyup="showHint(this.value)" /></form><p>建議:<span id="txtHint"></span></p> </body></html>

 

 ==============

執行個體解釋 - showHint() 函數

當使用者在上面的輸入框中鍵入字元時,會執行函數 "showHint()" 。該函數由 "onkeyup" 事件觸發:

function showHint(str){var xmlhttp;if (str.length==0)  {  document.getElementById("txtHint").innerHTML="";  return;  }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","gethint.asp?q="+str,true);xmlhttp.send();}
原始碼解釋:

如果輸入框為空白 (str.length==0),則該函數清空 txtHint 預留位置的內容,並退出函數。

如果輸入框不為空白,showHint() 函數執行以下任務:

  • 建立 XMLHttpRequest 對象
  • 當伺服器響應就緒時執行函數
  • 把請求發送到伺服器上的檔案
  • 請注意我們向 URL 添加了一個參數 q (帶有輸入框的內容)
AJAX 伺服器頁面 - ASP 和 PHP

由上面的 JavaScript 調用的伺服器頁面是 ASP 檔案,名為 "gethint.asp"。

下面,我們建立了伺服器檔案,用 ASP 編寫

ASP 檔案

"gethint.asp" 中的原始碼會檢查一個名字數組,然後向瀏覽器返回相應的名字:

<%response.expires=-1dim a(30)'用名字來填充數組a(1)="Anna"a(2)="Brittany"a(3)="Cinderella"a(4)="Diana"a(5)="Eva"a(6)="Fiona"a(7)="Gunda"a(8)="Hege"a(9)="Inga"a(10)="Johanna"a(11)="Kitty"a(12)="Linda"a(13)="Nina"a(14)="Ophelia"a(15)="Petunia"a(16)="Amanda"a(17)="Raquel"a(18)="Cindy"a(19)="Doris"a(20)="Eve"a(21)="Evita"a(22)="Sunniva"a(23)="Tove"a(24)="Unni"a(25)="Violet"a(26)="Liza"a(27)="Elizabeth"a(28)="Ellen"a(29)="Wenche"a(30)="Vicky"'獲得來自 URL 的 q 參數q=ucase(request.querystring("q"))'如果 q 大於 0,則尋找數組中的所有提示if len(q)>0 then  hint=""  for i=1 to 30    if q=ucase(mid(a(i),1,len(q))) then      if hint="" then        hint=a(i)      else        hint=hint & " , " & a(i)      end if    end if  nextend if'如果未找到提示,則輸出 "no suggestion"'否則輸出正確的值if hint="" then  response.write("no suggestion")else  response.write(hint)end if%>

聯繫我們

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