?
首先引入ajax.js文件 創建xmlhttpRequest對象


//創建XMLHttpRequest對象
var?xmlHttp;
function?newXMLHttpRequest()?{
????if?(window.XMLHttpRequest)?{
????????xmlHttp?=?new?XMLHttpRequest();
????}?else?if?(window.ActiveXObject)?{
????????try?{?
????????????xmlHttp?=?new?ActiveXObject("Msxml2.XMLHTTP");
????????}?catch?(e1)?{?
????????????try?{
????????????????xmlHttp?=?new?ActiveXObject("Microsoft.XMLHTTP");
????????????}?catch?(e2)?{
????????????}?
????????}
?????}
???????return?xmlHttp;
}?
//發起異步請求
function?sendRequest(){
????newXMLHttpRequest();
????var?url="AjaxHandler.ashx?name="+document.getElementById("txtName").value;
??????xmlHttp.open("GET",url,true);
????xmlHttp.onreadystatechange=onSuccessCallBack;
????xmlHttp.send(null);
}
//回調處理函數
function?onSuccessCallBack(){
????if?(xmlHttp.readyState?==?4)?
????{
????????if?(xmlHttp.status?==?200)?
????????{
????????????document.getElementById("result").innerHTML?=?xmlHttp.responseText;
????????}?
????????else?
????????{
????????????document.getElementById("result").innerHTML=result.status;
????????}
????}
}
//HTTP 處理程序
???IHttpHandler 接口:定義 ASP.NET 為使用自定義 HTTP 處理程序同步處理 HTTP Web 請求而實現的協定。
如果您的處理程序將訪問會話狀態值,它必須實現 IRequiresSessionState 接口(不包含任何方法的標記接口)。?
創建自定義 HTTP 處理程序
若要創建自定義 HTTP 處理程序,請創建實現 IHttpHandler 接口的類來創建一個同步處理程序。或者,可以實現 IHttpAsyncHandler 來創建一個異步處理程序。兩種處理程序接口都要求您實現 IsReusable 屬性和 ProcessRequest 方法。 IsReusable 屬性指定 IHttpHandlerFactory 對象(實際調用適當處理程序的對象)是否可以將處理程序放置在池中,并且重新使用它以提高性能。如果處理程序不能放在池中,則在每次需要處理程序時工廠都必須創建處理程序的新實例。
ProcessRequest 方法負責處理單個 HTTP 請求。在此方法中,將編寫生成處理程序輸出的代碼。
HTTP 處理程序有權訪問應用程序上下文。其中包括請求用戶的標識(如果已知)、應用程序狀態和會話信息。當請求 HTTP 處理程序時,ASP.NET 將調用相應處理程序的 ProcessRequest 方法。您在處理程序的 ProcessRequest 方法中編寫的代碼將創建一個響應,此響應隨后發送回請求瀏覽器。


using?System;
using?System.Collections;
using?System.Data;
using?System.Linq;
using?System.Web;
using?System.Web.Services;
using?System.Web.Services.Protocols;
namespace?Ajax
{
????///?<summary>
????///?$codebehindclassname$?的摘要說明
????///?</summary>
????[WebService(Namespace?=?"http://tempuri.org/")]
????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
????public?class?AjaxHandler?:?IHttpHandler
????{
????????public?void?ProcessRequest(HttpContext?context)
????????{
????????????context.Response.ContentType?=?"text/plain";//顯示html原代碼.
????????????//response.ContentType?="image/gif"?
????????????//response.ContentType?="image/jpeg"?
????????????//response.ContentType?="text/html"?
????????????string?name?=?context.Request.QueryString["name"];
????????????context.Response.Write(name.ToUpper());
????????}
????????public?bool?IsReusable
????????{
????????????get
????????????{
????????????????return?false;
????????????}
????????}
????}
}
前臺頁面:
?


<head?runat="server">
????<title>XMLHttpRequest+WebForm模式</title>
????<script?type="text/javascript"?src="Ajax.js"></script>
</head>
<body>
<input?type="text"?id="txtName"?/>
<input?type="button"?value="Request"?onclick="JavaScript:sendRequest();"?/>
<hr?/>
<div?id="result"></div>
</body>
</html>
或者通過客戶端向另一個頁面傳遞參數,由該頁面處理數據,把結果輸出到http流中?
?apsx.cs頁面
?? public partial class AjaxForm : System.Web.UI.Page
??? {
??????? protected void Page_Load(object sender, EventArgs e)
??????? {
??????????? if (!IsPostBack)
??????????? {
??????????????? string name = Request.QueryString["name"];
??????????????? Response.Write(name.ToUpper());
??????????????? Response.Flush();
??????????????? Response.End();
??????????? }
??????? }
??? }
//xmlhttpRequest對象
?? //發起異步請求
function sendRequest(){
??? newXMLHttpRequest();
??? var url="AjaxForm.aspx?name="+document.getElementById("txtName").value;
? ?xmlHttp.open("GET",url,true);
??? xmlHttp.onreadystatechange=onSuccessCallBack;
??? xmlHttp.send(null);
}