getJSON與aspx
準備工作
·Customer類
?
{
????public?int?Unid?{?get;?set;?}
????public?string?CustomerName?{?get;?set;?}
????public?string?Memo?{?get;?set;?}
????public?string?Other?{?get;?set;?}
}
?
?
(一)ashx
?
???????{?Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);
?
?
????$.getJSON(
????"webdata/Json_1.ashx",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????});
}
?
·通過getJSON向ashx請求數據。返回的數據為JSON對象。
(二)ashx文件,但返回的是實體集合
?
????{?Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
Customer?customer2?=?new?Customer?
????{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機星",?Other?=?"智多星"?};????????
List<Customer>?_list?=?new?List<Customer>();
_list.Add(customer);
_list.Add(customer2);????????
string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(_list);
context.Response.Write(strJson);
?
?
?
?
????$.getJSON(
????"webdata/Json_1.ashx",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????});
}
?
具體可以參看:http://www.cnblogs.com/jams742003/archive/2009/12/25/1632276.html
(三)請求aspx文件
·cs文件
?
{
???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文件
?
????$.getJSON(
????"webdata/Json_1.aspx",
????function(data)?{
???????????????var?tt?=?"";
???????????????$.each(data,?function(k,?v)?{
????????????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????????????})
????????????????$("#divmessage").html(tt);
????});
}
?
這個部分與請求ashx文件時相同。
請求實體集合時,與ashx時相同,這里不做重復。
(四)請求文本文件
文本文件提供json字符串,由$.getJSON得到json對象。
·文本文件
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}
文本文件提供json串,對于json的組成格式,請參見其它文檔。對于這一實體json,會被忽略空行與空格。?
?
????$.getJSON(
????"webdata/Json_1.txt",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????});
}
?
解析的方法與其它的相同。
?
對于多行的如下:
文本:
[
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"},
{Unid:2,CustomerName:"吳用",Memo:"天機星",Other:"智多星"}
]
?
解析:
?
????$.getJSON(
????"webdata/Json_1.txt",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????});
}
?
與其它的相同。
(五)帶參數ajax請求
以ashx為例子,按客戶id來請求客戶。
·Ashx文件
?
???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<Customer>?_list?=?new?List<Customer>();
_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請求?
?
????$.getJSON(
????"webdata/Json_2.ashx",
????{?iUnid:?1?},
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????});
}
?
其中參數也是以k/v對格式發出。請求返回的可以看到:在服務端以Customer列表集合返回。
?
?在jquery庫中,getJSON其實是調用的:Query.get(url, data, callback, "json")
這點很重要。