Jquery的.post说解(一)
準(zhǔn)備工作
·Customer類
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:加載頁的地址
·data(optional):k/v對或序列化的字符串(.serialize()),參數(shù)
·callbakc(optional):數(shù)據(jù)成功加載后的執(zhí)行函數(shù)
·type(optional):請求返回的數(shù)據(jù)格式,串型
?
(一)ashx文件
(1)請求單實體數(shù)據(jù)
·Ashx文件,這里不對返回的數(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)請求實體集合
·ashx文件
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);
????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ù)的請求
·ashx文件
在前者基礎(chǔ)上添加了對請求參數(shù)的獲取語句,并添加了linq查詢
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對象[object,object],可以直接解析。
這種參數(shù)傳遞的方法是以k/v對格式傳遞,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"
);}
?
這個web方法返回一個單獨的字串。這是一個純正的字串,對于客戶端來說,這是一個object對象,但也可以理解為一個[object,object]對象,而它完整的數(shù)據(jù)格式可以理解為:{text: "Hello World"}
所以這里對它進行訪問,可以如下:
·data.text 這種方式對應(yīng)于Object.Property
·data["text"] 這種方式對應(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類型的數(shù)據(jù):
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}
但客戶端得到的數(shù)據(jù)卻是object類型,可以理解為[object,object],也就是
{text:’{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}’}
客戶端請求到的數(shù)據(jù)取到json字串,然后轉(zhuǎn)換為json對象,后進行解析。
所以,在請求web服務(wù)方法時,如果方法返回字串類型,先要通過data.text得到做為唯一k/v對的v值,也就是json字串,然后再進行下一步操作。
(3)通過串行化返回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"
);}
?
這個方法與(2)相同道理。
(4)客戶集
·ws
[WebMethod]public?string?GetCustomerList_Json()
{
????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);?
????????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"
);}
?
其實得到了json字串,也就能正常解析出來。主要是理解返回的數(shù)據(jù)對象的格式。
(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?=?"天機星",?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時,post函數(shù)的type部分不能以json格式請求返回。可以省略。
?
轉(zhuǎn)載于:https://www.cnblogs.com/jams742003/archive/2009/12/29/1634945.html
總結(jié)
以上是生活随笔為你收集整理的Jquery的.post说解(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 热烈欢呼:cnblogs.com博客园首
- 下一篇: 安装XAMPP, MYSQL 启动后自动