asp.net 中 Json、Jquery、Post简单使用
2012-03-26 14:57
483 查看
前台取得数据:
//数据库中有 typeid 和 typename 字段
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
var json;
$(document).ready(function () { //ready-start
$.post( //post请求开始
"/test1.ashx", { data1: new Date() }, function (text) {
json =JSON.parse(text); //字符串转换为JSON格式,重要!
var html = '';
$.each(json, function (Index, Item) {//遍历每条数据
html += '<div class="comment"><h4>index:' +Index + ",typeid:" +Item['typeid'] + ',typename:'
+ Item['typename'] + '</div>';
//或 html += '<div class="comment"><h4>index:' +Index + ",typeid:" +Item.typeid + ',typename:' +Item.typename
+ '</div>';
})
$('#testdiv').html(html);//给层 testdiv 赋值
$('#jsondata').html(text);//给层 jsondata 赋值
}
); //post请求结束
}); //ready-End
</script>
后台响应请求:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;// 引用Newtonsoft.Json,版本3.5
namespace EbookShop
{
/// <summary>
/// test1 的摘要说明
/// </summary>
public class test1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
BLL.booktypeBLL bll = new BLL.booktypeBLL();
DataTable dt = bll.GetPagedList(1,5,"","typeid desc").Tables[0];//查询数据库中的表数据
string str =
JsonConvert.SerializeObject(dt);//转换为json格式的字符串
//JsonSerializer jsonSerializer = new JsonSerializer();
//jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
context.Response.Write(str);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
结果 :
欢迎大家到我的新浪博客学习交流: http://blog.sina.com.cn/pukuimin
//数据库中有 typeid 和 typename 字段
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
var json;
$(document).ready(function () { //ready-start
$.post( //post请求开始
"/test1.ashx", { data1: new Date() }, function (text) {
json =JSON.parse(text); //字符串转换为JSON格式,重要!
var html = '';
$.each(json, function (Index, Item) {//遍历每条数据
html += '<div class="comment"><h4>index:' +Index + ",typeid:" +Item['typeid'] + ',typename:'
+ Item['typename'] + '</div>';
//或 html += '<div class="comment"><h4>index:' +Index + ",typeid:" +Item.typeid + ',typename:' +Item.typename
+ '</div>';
})
$('#testdiv').html(html);//给层 testdiv 赋值
$('#jsondata').html(text);//给层 jsondata 赋值
}
); //post请求结束
}); //ready-End
</script>
后台响应请求:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;// 引用Newtonsoft.Json,版本3.5
namespace EbookShop
{
/// <summary>
/// test1 的摘要说明
/// </summary>
public class test1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
BLL.booktypeBLL bll = new BLL.booktypeBLL();
DataTable dt = bll.GetPagedList(1,5,"","typeid desc").Tables[0];//查询数据库中的表数据
string str =
JsonConvert.SerializeObject(dt);//转换为json格式的字符串
//JsonSerializer jsonSerializer = new JsonSerializer();
//jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
context.Response.Write(str);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
结果 :
index:0,typeid:9,typename:体育/运动
index:1,typeid:8,typename:政治/军事
index:2,typeid:7,typename:动漫/幽默
index:3,typeid:6,typename:小说/文学
index:4,typeid:5,typename:成功/励志
[{"typeid":"9","typename":"体育/运动"},{"typeid":"8","typename":"政治/军事"},{"typeid":"7","typename":"动漫/幽默"},{"typeid":"6","typename":"小说/文学"},{"typeid":"5","typename":"成功/励志"}]欢迎大家到我的新浪博客学习交流: http://blog.sina.com.cn/pukuimin
相关文章推荐
- JQuery以POST方法从ASP.NET服务器获取Json数据完整示例
- Asp.net Mvc Ajax JsonResult简单使用
- 基于jquery的文本框与autocomplete结合使用(asp.net+json)
- asp.net :使用jquery 的ajax +WebService+json 实现无刷新去后台值
- 使用jQuery.Ajax向ASP.NET MVC控制器Post数据
- asp.net 使用ajax请求地址接口数据 ajax返回类型是json js get post请求数据
- ASP.NET jQuery 食谱12 (通过使用jQuery validation插件简单实现用户注册页面验证功能)
- asp.net中 使用jquery+ashx 做ajax,json做数据传输
- Jquery中getJSON在asp.net中的使用说明
- asp.net中 使用jquery+ashx 做ajax,json做数据传输
- asp.net中使用jquery的ajax来post数据的树控件
- 使用Jquery提交Json格式的数据到Asp.net程序的另类做法
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- asp.net中使用jquery的ajax来post数据的树控件
- ASP.net MVC使用Jquery-pager和Newtonsoft.Json进行分页
- asp.net中使用jQuery调用WebService返回JSON数据
- 关于ASP.NET下,JQuery+AJAX使用JSON返回对象集合List数据的总结
- Asp.Net使用POST方法最简单的实现
- JQuery AJAX & JSON 使用示例一步到位 (DEMO:ASP.NET + MSSQL 无限级树形菜单)
- asp.net :使用jquery 的ajax +WebService+json 实现无刷新去后台值