• 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>


     

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    《Programming in Lua 3》读书笔记(十)
    《Programming in Lua 3》读书笔记(九)
    《Programming in Lua 3》读书笔记(八)
    [原]NYOJ-括号匹配-2(java)
    [原]NYOJ-字符串替换-113
    [原]NYOJ-小光棍数-458
    [原]NYOJ-公约数和公倍数 -40
    [原]NYOJ-开灯问题-77
    [原]NYOJ-数的位数-69
    [原]NYOJ-大数阶乘-28
  • 原文地址:https://www.cnblogs.com/starcrm/p/1315991.html
Copyright © 2020-2023  润新知