首页 > web前端 > js教程 > 正文

Jquery中getJSON在asp.net中的使用说明_jquery

php中文网
发布: 2016-05-16 18:09:42
原创
987人浏览过

准备工作
·Customer类

ShopEx助理
ShopEx助理

一个类似淘宝助理、ebay助理的客户端程序,用来方便的在本地处理商店数据,并能够在本地商店、网上商店和第三方平台之间实现数据上传下载功能的工具。功能说明如下:1.连接本地商店:您可以使用ShopEx助理连接一个本地安装的商店系统,这样就可以使用助理对本地商店的商品数据进行编辑等操作,并且数据也将存放在本地商店数据库中。默认是选择“本地未安装商店”,本地还未安

ShopEx助理 0
查看详情 ShopEx助理
复制代码 代码如下:

public class Customer
{
public int Unid { get; set; }
public string CustomerName { get; set; }
public string Memo { get; set; }
public string Other { get; set; }
}


(一)ashx

复制代码 代码如下:

Customer customer = new Customer
{ Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);

复制代码 代码如下:

function GetCustomer_Ashx() {
$.getJSON(
"webdata/Json_1.ashx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt += k + ":" + v + "
";
})
$("#divmessage").html(tt);
});
}

·通过getJSON向ashx请求数据。返回的数据为JSON对象。
(二)ashx文件,但返回的是实体集合
复制代码 代码如下:

Customer customer = new Customer
{ Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" };
List _list = new List();
_list.Add(customer);
_list.Add(customer2);
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list);
context.Response.Write(strJson);



复制代码 代码如下:

function GetCustomerList() {
$.getJSON(
"webdata/Json_1.ashx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
$.each(v,function(kk, vv) {
tt += kk + ":" + vv + "
";
});
});
$("#divmessage").html(tt);
});
}

 (三)请求aspx文件
·cs文件
复制代码 代码如下:

protected void Page_Load(object sender, EventArgs e)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
Response.Write(strJson);
}



·Aspx文件
Inherits="webdata_Json_1" %>

前台文件只保留Page声明,其它全部删除。

·js文件

复制代码 代码如下:

function GetCustomer_Aspx() {
$.getJSON(
"webdata/Json_1.aspx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt += k + ":" + v + "
";
})
$("#divmessage").html(tt);
});
}

这个部分与请求ashx文件时相同。
请求实体集合时,与ashx时相同,这里不做重复。
(四)请求文本文件
文本文件提供json字符串,由$.getJSON得到json对象。
·文本文件
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}
文本文件提供json串,对于json的组成格式,请参见其它文档。对于这一实体json,会被忽略空行与空格。

复制代码 代码如下:

function GetCustomer_txt() {
$.getJSON(
"webdata/Json_1.txt",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt += k + ":" + v + "
";
})
$("#divmessage").html(tt);
});
}

解析的方法与其它的相同。

对于多行的如下:
文本:
复制代码 代码如下:

[
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"},
{Unid:2,CustomerName:"吴用",Memo:"天机星",Other:"智多星"}
]

解析:
复制代码 代码如下:


function GetCustomer_TxtList() {
$.getJSON(
"webdata/Json_1.txt",
function(data) {
var tt = "";
$.each(data, function(k, v) {
$.each(v, function(kk, vv) {
tt += kk + ":" + vv + "
";
});
});
$("#divmessage").html(tt);
});
}

与其它的相同。
(五)带参数ajax请求
以ashx为例子,按客户id来请求客户。
·Ashx文件
复制代码 代码如下:

if(context.Request["iUnid"]==null)
return;
context.Response.ContentType = "text/plain";
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" };
List _list = new List();
_list.Add(customer);
_list.Add(customer2);

int iCustomerId =Convert.ToInt32(context.Request["iUnid"]);
var cus = from q in _list
where q.Unid == iCustomerId
select q;
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(cus);
context.Response.Write(strJson);

·ajax请求

复制代码 代码如下:

function GetCustomer_AshxWithPara() {
$.getJSON(
"webdata/Json_2.ashx",
{ iUnid: 1 },
function(data) {
var tt = "";
$.each(data, function(k, v) {
$.each(v, function(kk, vv) {
tt += kk + ":" + vv + "
";
});
});
$("#divmessage").html(tt);
});
}

其中参数也是以k/v对格式发出。请求返回的可以看到:在服务端以Customer列表集合返回。

在jquery库中,getJSON其实是调用的:Query.get(url, data, callback, "json")
这点很重要。

大家都在看:

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号