Jquery的.post说解(一)
準(zhǔn)備工作
·Customer類(lèi)
public?class?Customer
{
????public?int?Unid?{?get;?set;?}
????public?string?CustomerName?{?get;?set;?}
????public?string?Memo?{?get;?set;?}
????public?string?Other?{?get;?set;?}
}
?
jQuery.post(?url,?[data],?[callback],?[type] )
?
·url:加載頁(yè)的地址
·data(optional):k/v對(duì)或序列化的字符串(.serialize()),參數(shù)
·callbakc(optional):數(shù)據(jù)成功加載后的執(zhí)行函數(shù)
·type(optional):請(qǐng)求返回的數(shù)據(jù)格式,串型
?
(一)ashx文件
(1)請(qǐng)求單實(shí)體數(shù)據(jù)
·Ashx文件,這里不對(duì)返回的數(shù)據(jù)做顯式的序列化。
?
Customer?customer?=?new?Customer?
??{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};
context.Response.Write(customer);
?
·ajax post
function?GetCustomer_Ashx()?{
????$.post(
????"webdata/post_1.ashx",
????function(data)?{
????????var?sx?=?$.JsonToObject(data);
????????var?tt?=?"";
????????$.each(sx,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????},
????"json"
);}
?
(2)請(qǐng)求實(shí)體集合
·ashx文件
Customer?customer?=?new?Customer?
????{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};
Customer?customer2?=?new?Customer?
????{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機(jī)星",?Other?=?"智多星"?};
?
????List<Customer>?_list?=?new?List<Customer>();
????_list.Add(customer);
????_list.Add(customer2);
????string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(_list);
????context.Response.Write(strJson);
?
·ajax post
function?GetCustomer_AshxList()?{
????$.post(
????"webdata/post_1.ashx",
????function(data)?{
????????var?jsonObjects?=?$.jsonToObject(data);
????????var?tt?=?"";
????????$.each(jsonObjects,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????},
????"json"
????);
}
?
(3)帶參數(shù)的請(qǐng)求
·ashx文件
在前者基礎(chǔ)上添加了對(duì)請(qǐng)求參數(shù)的獲取語(yǔ)句,并添加了linq查詢(xún)
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);
?
·ajax post
function?GetCustomer_AshxWithPara()?{
????$.post(
????"webdata/post_1.ashx",
????{?iUnid:?1?},
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????},
????"json"
);}
?
注意,這里返回的直接是json對(duì)象[object,object],可以直接解析。
這種參數(shù)傳遞的方法是以k/v對(duì)格式傳遞,post還有一種方式,那就是.serialize()之后的字串。
(二)Web Service
(1)Hello
·ws
[WebMethod]public?string?HelloWorld()
{
????return?"Hello?World";
}
?
·ajax post
function?WebService_Hello()?{
????$.post(
????"post_1.asmx/HelloWorld",
????function(data)?{
????????alert(data.text);
????????$("#divmessage").html(data);
????},
????"json"
);}
?
這個(gè)web方法返回一個(gè)單獨(dú)的字串。這是一個(gè)純正的字串,對(duì)于客戶(hù)端來(lái)說(shuō),這是一個(gè)object對(duì)象,但也可以理解為一個(gè)[object,object]對(duì)象,而它完整的數(shù)據(jù)格式可以理解為:{text: "Hello World"}
所以這里對(duì)它進(jìn)行訪(fǎng)問(wèn),可以如下:
·data.text 這種方式對(duì)應(yīng)于Object.Property
·data["text"] 這種方式對(duì)應(yīng)于Object["key"]
(2)json串
·ws
[WebMethod]public?string?HelloWorld_Json()
{
????string?strJson=
??????@"{Unid:1,CustomerName:""宋江"",Memo:""天魁星"",Other:""黑三郎""}";
????return?strJson;
}
?
·ajax post
function?WebService_HelloJsonString()?{
????$.post(
????"post_1.asmx/HelloWorld_Json",
????function(data)?{
????????var?jsonString?=?data.text;
????????var?jsonObject?=?$.jsonToObject(jsonString);
????????var?tt?=?"";
????????$.each(jsonObject,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????},
????"json"
);}
?
雖然服務(wù)方法返回的是string類(lèi)型的數(shù)據(jù):
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}
但客戶(hù)端得到的數(shù)據(jù)卻是object類(lèi)型,可以理解為[object,object],也就是
{text:’{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}’}
客戶(hù)端請(qǐng)求到的數(shù)據(jù)取到json字串,然后轉(zhuǎn)換為json對(duì)象,后進(jìn)行解析。
所以,在請(qǐng)求web服務(wù)方法時(shí),如果方法返回字串類(lèi)型,先要通過(guò)data.text得到做為唯一k/v對(duì)的v值,也就是json字串,然后再進(jìn)行下一步操作。
(3)通過(guò)串行化返回json字串的web方法
·ws
[WebMethod]
public?string?GetCustomer_Json()
{
????Customer?customer?=?new?Customer
??????{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};
????string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(customer);
????return?strJson;
}
?
·ajax post
function?WebService_CustomerJsonString()?{
????$.post(
????"post_1.asmx/GetCustomer_Json",
????function(data)?{
????????var?jsonString?=?data.text;
????????var?jsonObject?=?$.jsonToObject(jsonString);
????????var?tt?=?"";
????????$.each(jsonObject,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????},
????"json"
);}
?
這個(gè)方法與(2)相同道理。
(4)客戶(hù)集
·ws
[WebMethod]public?string?GetCustomerList_Json()
{
????Customer?customer?=?new?Customer?
???????{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};
????Customer?customer2?=?new?Customer?
???????{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機(jī)星",?Other?=?"智多星"?};?
????????List<Customer>?_list?=?new?List<Customer>();
????????_list.Add(customer);
????????_list.Add(customer2);?
????????string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(_list);
????????return?strJson;
}
?
·ajax post
function?WebService_CustomerListJsonString()?{
????$.post(
????"post_1.asmx/GetCustomerList_Json",
????function(data)?{
????????var?jsonString?=?data.text;
????????var?jsonObject?=?$.jsonToObject(jsonString);
????????var?tt?=?"";
????????$.each(jsonObject,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????},
????"json"
);}
?
其實(shí)得到了json字串,也就能正常解析出來(lái)。主要是理解返回的數(shù)據(jù)對(duì)象的格式。
(5)帶參數(shù)的ws
·ws
[WebMethod]public?string?GetCustomerList_JsonPara(int?iUnid)
{
????Customer?customer?=?new?Customer?
???????{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};
????Customer?customer2?=?new?Customer?
???????{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機(jī)星",?Other?=?"智多星"?};?
????????List<Customer>?_list?=?new?List<Customer>();
????????_list.Add(customer);
????????_list.Add(customer2);?
????????var?cus?=?from?q?in?_list
??????????????????where?q.Unid?==?iUnid
??????????????????select?q;?
????????string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(cus);
????????return?strJson;
}
?
?·ajax post
function?WebService_CustomerListJsonStringWithPara()?{
????$.post("post_1.asmx/GetCustomerList_JsonPara",
????{iUnid:2},
????function(data)?{
????????var?jsonString?=?data.text;
????????var?jsonObject?=?$.jsonToObject(jsonString);
????????var?tt?=?"";
????????$.each(jsonObject,?function(k,?v)?{
????????$.each(v,?function(kk,?vv)?{
????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????});
????????});
????????$("#divmessage").html(tt);????????
????}
?);}
?
帶參數(shù)的post時(shí),post函數(shù)的type部分不能以json格式請(qǐng)求返回??梢允÷?。
?
轉(zhuǎn)載于:https://www.cnblogs.com/jams742003/archive/2009/12/29/1634945.html
總結(jié)
以上是生活随笔為你收集整理的Jquery的.post说解(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 热烈欢呼:cnblogs.com博客园首
- 下一篇: 写给那些正奔三的80后[转]