jQuery操作ajax处理json数据[Demo]

添加人:happyjlaaa一级(220分)   添加时间:2008-07-25    阅读次数:1001  收藏此教程

Default.html前台页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>无标题页</title>
    <script type="text/javascript" src="js/jquery-1.2.1.pack.js"></script>
</head>
<script type="text/javascript">
    $(document).ready(function (){
        $("#btnOK").click(function (){
            $.getJSON(
                "Handler.ashx",
                {},
                function(json){
                    $("#list").append("<li>id:"+json.EmployeeId+"|Name:"+json.EmployeeName+"|年龄:"+json.EmployeeInfo[0]+"|身高:"+json.EmployeeInfo[1]+"|体重:"+json.EmployeeInfo[2]+"</li>");
                }
            )
        })
    })
</script>
<body>
    <input id="btnOK" value="加载数据" type="button"/>
    <ul id="list">
       
    </ul>
</body>
</html>

---------------------
Handler.ashx服务器端处理请求的代码
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Runtime.Serialization;
using Newtonsoft.Json;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write(ReturnResult()); 
    }

    public string ReturnResult() {
        Employee employee = new Employee();
        employee.EmployeeId = 1;
        employee.EmployeeName = "yang";
        employee.EmployeeInfo = "25,170cm,55kg".Split(',');
        string jsonstr = JavaScriptConvert.SerializeObject(employee);
        return jsonstr;
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

    class Employee
    {
        public int EmployeeId;
        public string EmployeeName;
        private string[] employeeInfo;
        public string[] EmployeeInfo
        {
            get { return employeeInfo; }
            set { employeeInfo = value; }
        }
    }
}

1页 第1上一页1下一页
相关的教程: jquery ajax json
收藏此教程

当前平均分: 1.0(10 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: