標籤:
1 <head runat="server"> 2 <title>簡單的ajax調用</title> 3 <script type="text/javascript"> 4 function Ajax() { 5 var xmlHttpReq = null; 6 //IE5,IE6是以ActiveXObject引入XMLHttpRequest對象 7 //這個判斷是為了相容上述兩個版本的IE瀏覽器 8 if (window.ActiveXObject) { 9 xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");10 }11 //XMLHttpRequest是window對象的子物件12 else if (window.XMLHttpRequest) {13 xmlHttpReq = new XMLHttpRequest();14 }15 16 xmlHttpReq.open("GET", "AjaxServer.aspx", true);17 //XMLHttpRequest對象的readyState值改變值,會觸發onreadystatechange事件18 xmlHttpReq.onreadystatechange = function () {19 //請求完成載入:readyState=420 //HTTP狀態值為20021 if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {22 var resText = document.getElementById("resText");23 //responseText是伺服器端返回的結果24 //即AjaxServer.aspx後台返回的結果25 resText.innerHTML = xmlHttpReq.responseText;26 }27 };28 //使用GET方法提交,所以可以使用null作為參數調用29 xmlHttpReq.send(null);30 }31 </script>32 </head>33 <body>34 <form id="form1" runat="server">35 <input type="button" value="Ajax提交" onclick="Ajax();" />36 <div id="resText">37 </div>38 </form>39 </body>View Code
1 public partial class AjaxServer : System.Web.UI.Page2 {3 protected void Page_Load(object sender, EventArgs e)4 {5 Response.Write("hello,Ajax!");6 }7 }8 }View Code
:
Ajax學習筆記(1)