asp.net + html頁面來實現 ajax 示意——給初學者的參考

來源:互聯網
上載者:User

標籤:blog   http   os   java   io   ar   資料   2014   cti   

靜態頁面示意代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax實驗</title>
<script type="text/javascript">
function btnClick() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //建立xmlhttp對象,針對ie瀏覽器寫的,其他瀏覽器方法不同
if (!xmlhttp) {
alert("建立對象失敗");
return false;
}
xmlhttp.open("POST", "GetDate1.ashx?tx" + new Date(), false); //準備向伺服器的getdate1.ashx發出Post請求
//xmlhttp的open是非同步請求,不會等到拿到資料才返回,因此需要監聽xmlhttp對象的onreadystatechange事件
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4) { //xmlhttp執行狀態之一,1正在載入對象,2載入完畢,3正在擷取資料,4返回資料
if(xmlhttp.status==200) { //傳回碼200,為成功,http響應代碼
document.getElementById("Text1").value=xmlhttp.responseText;//responeText是伺服器返回的文本
}
else{
alert("ajax伺服器返回錯誤!");
}
}
}
xmlhttp.send(); //這是才象伺服器發送請求
}
</script>
</head>
<body>
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="btnClick()" />
</body>
</html>

 

一般處理常式示意代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ajax測試1
{
/// <summary>
/// GetDate1 的摘要說明
/// </summary>
public class GetDate1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(DateTime.Now.ToString());
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

 

asp.net + html頁面來實現 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.