首先编写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 >
    <div>
    <input />
   <ul ></ul>
    </div>
    </form>
</body>
</html>


 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2021-05-30
  • 2021-06-03
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2021-07-25
  • 2021-09-25
  • 2021-10-06
  • 2021-08-14
  • 2021-05-22
相关资源
相似解决方案