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

 

首先引入ajax.js文件 创建xmlhttpRequest对象

ContractedBlock.gif ExpandedBlockStart.gif 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 方法中编写的代码将创建一个响应,此响应随后发送回请求浏览器。

ContractedBlock.gif ExpandedBlockStart.gif 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;
            }
        }
    }
}

前台页面:

 

ContractedBlock.gif ExpandedBlockStart.gif 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值