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

 

首先编写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>


 

 

posted @ 2008-10-21 15:38  昕友软件开发  阅读(681)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯