一個最簡單的AJAX例子

該例子實現的功能:通過點擊Button從服務器端獲取數據然後提示輸出...

Default.aspx

  1. <html>
  2. <head runat="server">
  3.     <meta http-equiv="Content-Type" content="text/html; charset=big5" />
  4.     <title></title>
  5.     <script language="javascript" type="text/javascript"
  6. var request; 
  7. function createRequest() { 
  8.   try { 
  9.     request = new XMLHttpRequest(); 
  10.   } catch (trymicrosoft) { 
  11.     try { 
  12.       request = new ActiveXObject("Msxml2.XMLHTTP"); 
  13.     } catch (othermicrosoft) { 
  14.       try { 
  15.         request = new ActiveXObject("Microsoft.XMLHTTP"); 
  16.       } catch (failed) { 
  17.         request = false
  18.       } 
  19.     } 
  20.   } 
  21.   if (!request) 
  22.     alert("Error initializing XMLHttpRequest!"); 
  23. function getCustomerInfo() { 
  24.   createRequest(); 
  25.   request.open("get","aaa.aspx",true);
  26.   request.onreadystatechange =updatePage;
  27.   request.send(null);
  28. function updatePage()
  29. {
  30.     if(request.readyState==4)
  31.     {
  32.       if(request.status==200)
  33.       {
  34.         alert(request.responseText);
  35.       }
  36.     }
  37. }
  38. </script> 
  39. </head>
  40. <body>
  41.     <form runat="server" action="">
  42.         <input id="Button1" type="button" value="button" onclick="getCustomerInfo();"/>
  43.     </form>
  44. </body>
  45. </html>

 

aaa.aspx.cs

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class nameSpace08108_aaa : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         Response.Write("hello!");
  16.         Response.End();
  17.     }
  18. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章