日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

XMLHttpRequest+WebForm模式(接口IHttpHandler)实现ajax

發布時間:2025/3/8 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XMLHttpRequest+WebForm模式(接口IHttpHandler)实现ajax 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

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

Code
//創建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 方法中編寫的代碼將創建一個響應,此響應隨后發送回請求瀏覽器。

Code
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;
????????????}
????????}
????}
}

前臺頁面:

?

Code
<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);
}

轉載于:https://www.cnblogs.com/hubcarl/archive/2009/09/15/1567251.html

總結

以上是生活随笔為你收集整理的XMLHttpRequest+WebForm模式(接口IHttpHandler)实现ajax的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。