Handler一般处理程序的应用--结合jQuery

本文介绍了一个简单的示例,展示了如何使用ASP.NET编写一个处理文件来生成JSON数据,并通过jQuery发送AJAX请求来获取和显示这些数据。示例中包含了服务端代码和前端HTML页面的完整实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

首先编写handle处理文件:

 

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

namespace Reflect
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //接受参数name和age   
            string data = "[{name:\"" + context.Request.QueryString.GetValues("name")[0] + "\",age:" + context.Request.QueryString.GetValues("age")[0] + "},{name:\"zhangsan\",age:23}]";
            //构建的json数据
            context.Response.Write(data);

        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

 

前台代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>jquery获取json数据演示页面</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    function getData(){
    $("#list").html("");//清空列表中的数据
   //发送ajax请求
    $.getJSON(
    "handler1.ashx",//产生JSON数据的服务端页面
    {name:"haha",age:40},//向服务器发出的查询字符串(此参数可选)
   //对返回的JSON数据进行处理,本例以列表的形式呈现
    function(json){
   //循环取json中的数据,并呈现在列表中
    $.each(json,function(i){
    $("#list").append("<li>name:"+json[i].name+"&nbsp; Age:"+json[i].age+"</li>")
    })
    })
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="获取数据" onclick="getData()" />
   <ul id="list"></ul>
    </div>
    </form>
</body>
</html>


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值