【www.gdgbn.com--jquery】

jquery.getjson详解与getjson和asp教程.net实例
jquery.getjson( url, [data], [callback] ) 跨域加载json数据。
url: 发送请求的地址
data : (可选) 待发送key/value参数
callback: (可选) 载入成功时的回调函数
主要用于客户端获取服务器json数据。简单示例:
服务器脚本,返回json数据:
$.getjson.php教程
$arr=array("name"=>"zhangsan", "age"=>20);
$jarr=json_encode($arr);
echo $jarr;
注意两点:
第一:在返回客户端之前,先用php函数json_encode将要返回的数据进行编码。
第二:返回到客户端用的是echo,而不是return。
下面是核心的客户端代码:

$.getjson.html
 代码如下:
<script language="网页特效" type="text/javascript" src="./js/jquery.js"></script>
<script language="javascript" type="text/javascript">
function getjs()
{
$.getjson("$.getjson.php", {}, function(response){
alert(response.age);
});
}

下面看与asp.net教程 中getjson的应用

准备工作
·customer类

 代码如下:

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文件

<%@ page language="c#" autoeventwireup="true" codefile="json_1.aspx.cs"
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列表集合返回。

注意一点:
由于在php中是用json编码返回值,所以此处必须用getjson去调用php文件,从而获取数据。同时可以注意到,经由getjson得到的数据已经变成了一个对象数组,可以用response.name,response.age很直观的获取返回值。

 

本文来源:http://www.gdgbn.com/wangyezhizuo/29418/