標籤: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 示意——給初學者的參考