AJAX ASP.NET ashx用法
ajax asp.net ashx用法
前端页面:
<%@ 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>
后端类代码:
<%@ 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"; } }
<%@ 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";
}
}
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05