<%@ WebHandler Language="C#" Class="Handler"%>
using System;
using System.Web;
public class Handler: IHttpHandler
{
public void ProcessRequest(HttpContext context){
context.Response.ContentType ="text/json";// context.Response.Write("{\"msg\":\"Hello World" + context.Request["name"] + "来自handler.ashx\"}");
context.Response.Write("{\"msg\":\"Hello World"+ context.Request["name"]+"来自handler.ashx\"}");
context.Response.End();}
public bool IsReusable
{
get
{returnfalse;}}}
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ajax传递_Default"%><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><script type="text/javascript" src="../../Scripts/jquery-3.4.1.js"></script><script type="text/javascript" src="../Scripts/jquery-3.4.1.js"></script><title></title></head><body><form id="form1" runat="server"><div><input type ="button" id ="b1" value="按钮b1"/><input type ="button" id ="c1" value="按钮c1"/><input type ="text" id ="Text1" value="张三"><input name="funItem" value="1" type="checkbox" id="ck1"/>c#
<input name="funItem" value="2" type="checkbox"/>html
<input name="funItem" value="3" type="checkbox"/>javascript
</div></form></body></html><script>
$("#b1").bind("click",function(){
$.ajax({
type:"post",
url:"Handler.ashx",
data:"name="+ $("#Text1").val(),
success:function( result ){alert( result.msg );}});});</script><script type="text/javascript">
$("#c1").bind("click",function(){
$.ajax({
type:"post",
url:"Handler.ashx",
data:"name="+ $("#ck1").val(),
success:function( result ){alert( result.msg );}});});</script>