实战:当jquery遇上了json

jquery :大家一定很熟悉,就算没用过,也应该听说过,不多做介绍,不知道的赶紧google一下。
json :一种数据格式,她小巧,轻盈。在javascript的世界中,完胜XML。
jquery和json都是因为小与巧而被广泛使用,今天把她们弄到一起,大家应该没意见吧。 实战:当jquery遇上了json - 往 - 郁郁的博客

废话了一把,现在言归正传,
通过这篇文章你可以得到以下收获:
1.jqury如何用ajax的形式调用后台asp.net页面生成的json数据
2.jquery简单的dom操作
3.送本jquery的开发手册给大家(大家慢慢去研究)

准备工作:
首先,我们新建个网站(.net2.0就行).
1.在我们的项目中jquery的js文件。
2.新建一个htm文件,命名为dome.htm吧。
代码如下:(head区的js代码就是实现的全部代码,有详细注释)
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客 Code
实战:当jquery遇上了json - 往 - 郁郁的博客 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
实战:当jquery遇上了json - 往 - 郁郁的博客
实战:当jquery遇上了json - 往 - 郁郁的博客 <html xmlns="http://www.w3.org/1999/xhtml">
实战:当jquery遇上了json - 往 - 郁郁的博客 <head>
实战:当jquery遇上了json - 往 - 郁郁的博客     <title>jquery获取json数据演示页面</title>
实战:当jquery遇上了json - 往 - 郁郁的博客     <script type="text/javascript" src="js/jquery-1.2.1.pack.js"></script>
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客     <script type="text/javascript"> 实战:当jquery遇上了json - 往 - 郁郁的博客
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客     function getData() 实战:当jquery遇上了json - 往 - 郁郁的博客 {
实战:当jquery遇上了json - 往 - 郁郁的博客     $("#list").html("");//清空列表中的数据
实战:当jquery遇上了json - 往 - 郁郁的博客    //发送ajax请求
实战:当jquery遇上了json - 往 - 郁郁的博客     $.getJSON(
实战:当jquery遇上了json - 往 - 郁郁的博客     "jsondata.ashx",//产生JSON数据的服务端页面
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客      实战:当jquery遇上了json - 往 - 郁郁的博客 {name:"test",age:20},//向服务器发出的查询字符串(此参数可选)
实战:当jquery遇上了json - 往 - 郁郁的博客    //对返回的JSON数据进行处理,本例以列表的形式呈现
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客     function(json) 实战:当jquery遇上了json - 往 - 郁郁的博客 {
实战:当jquery遇上了json - 往 - 郁郁的博客    //循环取json中的数据,并呈现在列表中
实战:当jquery遇上了json - 往 - 郁郁的博客 实战:当jquery遇上了json - 往 - 郁郁的博客     $.each(json,function(i) 实战:当jquery遇上了json - 往 - 郁郁的博客 {
实战:当jquery遇上了json - 往 - 郁郁的博客     $("#list").append("<li>name:"+json .name+"&nbsp; Age:"+json.age+"</li>")
实战:当jquery遇上了json - 往 - 郁郁的博客    })
实战:当jquery遇上了json - 往 - 郁郁的博客    })
实战:当jquery遇上了json - 往 - 郁郁的博客    }
实战:当jquery遇上了json - 往 - 郁郁的博客    </script>
实战:当jquery遇上了json - 往 - 郁郁的博客</head>
实战:当jquery遇上了json - 往 - 郁郁的博客<body>
实战:当jquery遇上了json - 往 - 郁郁的博客    <input id="Button1" type="button" value="获取数据" οnclick="getData()" />
实战:当jquery遇上了json - 往 - 郁郁的博客   <ul id="list"></ul>
实战:当jquery遇上了json - 往 - 郁郁的博客</body>
实战:当jquery遇上了json - 往 - 郁郁的博客</html>

3.我们再建一个一般应用程序(jsonData.ashx)
代码如下:
实战:当jquery遇上了json - 往 - 郁郁的博客实战:当jquery遇上了json - 往 - 郁郁的博客Code
实战:当jquery遇上了json - 往 - 郁郁的博客<%@ WebHandler Language="C#" Class="jsonData" %>
实战:当jquery遇上了json - 往 - 郁郁的博客
实战:当jquery遇上了json - 往 - 郁郁的博客using System;
实战:当jquery遇上了json - 往 - 郁郁的博客using System.Web;
实战:当jquery遇上了json - 往 - 郁郁的博客
实战:当jquery遇上了json - 往 - 郁郁的博客实战:当jquery遇上了json - 往 - 郁郁的博客public class jsonData : IHttpHandler 实战:当jquery遇上了json - 往 - 郁郁的博客{
实战:当jquery遇上了json - 往 - 郁郁的博客    
实战:当jquery遇上了json - 往 - 郁郁的博客实战:当jquery遇上了json - 往 - 郁郁的博客    public void ProcessRequest (HttpContext context) 实战:当jquery遇上了json - 往 - 郁郁的博客{
实战:当jquery遇上了json - 往 - 郁郁的博客        context.Response.ContentType = "text/plain";
实战:当jquery遇上了json - 往 - 郁郁的博客        string data = "[{name:\"ants\",age:24},{name:\"lele\",age:23}]";//构建的json数据
实战:当jquery遇上了json - 往 - 郁郁的博客       //下面两句是用来测试前台向此页面发出的查询字符
实战:当jquery遇上了json - 往 - 郁郁的博客        string querystrname = context.Request.QueryString.GetValues("name")[0];//取查询字符串中namer的值
实战:当jquery遇上了json - 往 - 郁郁的博客        string querystage = context.Request.QueryString.GetValues("age")[0];//取查询字符串中age的值
实战:当jquery遇上了json - 往 - 郁郁的博客        
实战:当jquery遇上了json - 往 - 郁郁的博客        context.Response.Write(data);
实战:当jquery遇上了json - 往 - 郁郁的博客    }
实战:当jquery遇上了json - 往 - 郁郁的博客 
实战:当jquery遇上了json - 往 - 郁郁的博客实战:当jquery遇上了json - 往 - 郁郁的博客    public bool IsReusable 实战:当jquery遇上了json - 往 - 郁郁的博客{
实战:当jquery遇上了json - 往 - 郁郁的博客实战:当jquery遇上了json - 往 - 郁郁的博客        get 实战:当jquery遇上了json - 往 - 郁郁的博客{
实战:当jquery遇上了json - 往 - 郁郁的博客            return false;
实战:当jquery遇上了json - 往 - 郁郁的博客        }
实战:当jquery遇上了json - 往 - 郁郁的博客    }
实战:当jquery遇上了json - 往 - 郁郁的博客
实战:当jquery遇上了json - 往 - 郁郁的博客}

对以上的内容我只说一点,那就是前台页面中的$.getJSON方法 

$.getJSON(url, params, callback) 
用一个HTTP GET请求一个JavaScript JSON数据 
返回值:XMLHttpRequest
参数:
url (String): 装入页面的URL地址。 
params (Map): (可选)发送到服务端的键/值对参数。
callback (Function): (可选) 当数据装入完成时执行的函数.  

下面贴一些运行成功的图:
1.运行结果
实战:当jquery遇上了json - 往 - 郁郁的博客
2,后台调试的数据:
实战:当jquery遇上了json - 往 - 郁郁的博客

好啦,这篇文章就写到这。
附上jquery的开发手册:jqueryapi12.rar(这个手册本身就是基于jquery做的,,很漂亮)
文字显得有点啰嗦,应该没走题,请大家给个及格分啦,实战:当jquery遇上了json - 往 - 郁郁的博客


原创文字只代表本人某一时间内的观点或结论,本人不对涉及到的任何代码担保。转载请标明出处!
支持我请点击上方广告,谢谢^0^ Tag标签: asp.net,Jquery,json
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值