AJAX ASP.NET ashx用法

ajax asp.net ashx用法

前端页面:

[html]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajax.aspx.cs" Inherits="About_ajax" %>  
      
    <!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 runat="server">      
         <title>Jquery Ajax实例</title>    
       <script src="../jQuery/js141/jquery-1.4.1.js" type ="text/javascript"></script>  
        <script src="../jQuery/js141/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>  
        <script type="text/javascript">  
            $(document).ready(function() {  
                $("#dbtn").click(function() {  
                    $.ajax({  
                        type: "POST",  
                        //dataType:"Text",    
                        url: "AjaxHandler.ashx",  
                        data: { name: "admin", pass: "admin" },  
                        beforeSend: function() { $("#ds").html("loading"); },  
                        success: function(msg) { $("#ds").html("<p>" + msg + "</p>"); }  
                    });  
                });  
                //  
                $("#btn_cbfbh").click(function() {  
                    //var zbm = '111';  
                    //alert(zbm);  
                    $.ajax({  
                        type: "POST",  
                        //dataType:"Text",    
                        url: "S_CBFBM.ashx",  
                        data: { ZBM: "51011200100200" },  
                        beforeSend: function() {  
                            //$("#div_load").visible = true;  
                        },  
                        success: function(msg) {  
                            //$("#div_load").visible = false;  
                            $("#ds").html("<p>" + msg + "</p>");  
                            $("#CBFBM").val(msg);  
                        }  
                    });  
                });  
                //  
            });  
            function js_function_get_cbfbm(p_zdm) {  
                $.ajax({  
                    type: "POST",  
                    url: "S_CBFBM.ashx",  
                    data: { ZBM: p_zdm },  
                    beforeSend: function() {  
                        //$("#div_load").visible = "true;  
                    },  
                    success: function(msg) {  
                        //$("#div_load").visible = false;  
                        $("#ds").html("<p>" + msg + "</p>");  
                        $("#CBFBM").val(msg);  
                    }  
                });  
            };  
        </script>  
    </head>  
    <body onload="javascript:{js_function_get_cbfbm('sfsfds');}">  
        <form id="form1" runat="server">  
        <div>  
          <div id="ds"><p>我是AJAX原来的文字!</p></div>    
          <input type="button" value="提交AJAX测试" id="dbtn" name="dbtn" />    
          <br />  
          <input type="text"  id="CBFBM" name="CBFBM"/>     
          <input type="button" value="获取成包方编号" id="btn_cbfbh" name="btn_cbfbh" />   
          <br />  
          <div id="div_load" ><p></p></div>       
        </div>   
        <script type="text/javascript">  
              var zbm='';  
              if(zbm=='') zbm='51011200100900';  
              //js_function_get_cbfbm(zbm);  
        </script>   
        </form>  
    </body>  
    </html>

 
后端类代码:

[csharp]
<%@ WebHandler Language="C#" Class="AjaxHandler" %>  
      
    using System;  
    using System.Web;  
      
    public class AjaxHandler : IHttpHandler {  
          
        public void ProcessRequest (HttpContext context) {  
            //context.Response.ContentType = "text/plain";  
            //context.Response.Write("Hello World");  
            context.Response.ContentType = "text/plain";  
      
            //context.Response.Write("Hello World");    
            if (context.Request["name"].ToString() == "admin" &&  
                context.Request["pass"].ToString() == "admin")  
            {  
                context.Response.Write("Y");  
            }  
            else  
            {  
                context.Response.Write("N");  
            }   
        }  
       
        public bool IsReusable {  
            get {  
                return false;  
            }  
        }  
      
        [System.Web.Services.WebMethod]  
        public static string SayHello()  
        {  
            return "Hello Ajax! AjaxHandler.ashx";  
        }   
      
    }
[csharp]
<%@ WebHandler Language="C#" Class="S_CBFBM" %>  
  
using System;  
using System.Web;  
  
/// <summary>  
/// 功能:获取 编号  
/// vp:hsg  
/// create date:2012-11-23  
/// </summary>  
public class S_CBFBM : IHttpHandler {  
      
    public void ProcessRequest (HttpContext context)   
    {          
        context.Response.ContentType = "text/plain";  
                    
        string zbm=context.Request["ZBM"].ToString();  
        zbm=zbm.Trim();  
        if(zbm!="")   
        {  
            string cbfbm = zbm + "001";  
            context.Response.Write(cbfbm);  
        }  
        else  
        {  
            context.Response.Write("");  
        }   
    }  
   
    public bool IsReusable   
    {  
        get   
        {  
            return false;  
        }  
    }  
  
    [System.Web.Services.WebMethod]  
    public static string SayHello()  
    {  
        return "Hello Ajax! S_CBFBM.ashx";  
    }   
  
}

相关推荐