jQuery 结合 Json 提交数据到Webservice,并接收从Webservice返回的Json数据
jQuery ajax ?webservice:get 和 post
一、GET 方式 客戶端代碼????????????var?data?=?{?classCode:?"0001"};?//?這里要直接使用JOSN對(duì)象????????????$.ajax({
????????????????type:?"GET",
????????????????contentType:?"application/json;?charset=utf-8",
????????????????url:?"/WebServices/ProductPropertyWebService.asmx/GetProductPropertyList",
????????????????dataType:?"json",
????????????????anysc:?false,
????????????????data:?data,
????????????????success:?RenderProperties,
????????????????error:?function?(XMLHttpRequest,?textStatus,?errorThrown)?{
????????????????????alert(errorThrown?+?':'?+?textStatus);??//?錯(cuò)誤處理
????????????????}
????????????});?
?
服務(wù)器端 代碼?????[ScriptMethod(ResponseFormat?=?ResponseFormat.Json,?UseHttpGet?=?true)]?//UseHttpGet?=?true????????public?List<Property>?GetProductPropertyList()
????????{
????????????string?classCode?=?HttpContext.Current.Request["classCode"];?//?Get?方式,要在查詢字符串里得到參數(shù)值
????????????return?PropertyManager.GetPropertySet(classCode,?"zh-CN").DataList;
????????}
?
?
二、POST 方式 客戶端 代碼????????????var?data?=?'{?classCode:?"'?+?classCode?+?'",?city:?"GuangDong"?}';????//?這里要使用拼接好的JOSN字符串????????????$.ajax({
????????????????type:?"POST",
????????????????contentType:?"application/json;?charset=utf-8",
????????????????url:?"/WebServices/ProductPropertyWebService.asmx/GetProductPropertyList",
????????????????dataType:?"json",
????????????????anysc:?false,
????????????????data:?data,?//?Post?方式,data參數(shù)不能為空"",如果不傳參數(shù),也要寫(xiě)成"{}",否則contentType將不能附加在Request?Headers中。
????????????????success:?RenderProperties,
????????????????error:?function?(XMLHttpRequest,?textStatus,?errorThrown)?{
????????????????????alert(errorThrown?+?':'?+?textStatus);?//?錯(cuò)誤處理
????????????????}
????????????});
?
服務(wù)器端 代碼?????[ScriptMethod(ResponseFormat?=?ResponseFormat.Json,?UseHttpGet?=?false)]?//?UseHttpGet?=?false????????public?List<Property>?GetProductPropertyList(string?classCode,?string?city)?//?Post?方式,參數(shù)對(duì)應(yīng)JSON字段屬性,并自動(dòng)賦值直接使用
????????{
????????????return?PropertyManager.GetPropertySet(classCode,?"zh-CN").DataList;
????????} 注意:GET方法與POST方法不同,有參數(shù)的時(shí)候,如果參數(shù)的值不是ASCII字符(比如中文),GET的參數(shù)要encodeURI編碼,要不服務(wù)端接收到的數(shù)據(jù)為亂碼。
?
?復(fù)雜的Json數(shù)據(jù)提交?
?簡(jiǎn)單的Json 格式的數(shù)據(jù)如 { name:Yangjun, age:27 }
?復(fù)雜的Json格式的數(shù)據(jù),其實(shí)只是Json嵌套,比如: {name:yangjun, age:27, child:[{name:yangke, age:1},{name:yangbin, age:2}]}
?如果是這種復(fù)雜的Json格式的數(shù)據(jù)要提交,并在Webservices中獲取,然后根據(jù)這個(gè)Json格式的字符串,序列成.net對(duì)象,應(yīng)該怎么做呢??
比如我要提交下面的數(shù)據(jù):
客戶端:
?
代碼var?productPropertyTemplate?=??{"ProductId":10024,?"PropertyList":[{"PropertyId":18,?"PropertyType":"text",?"PropertyValue":"號(hào)碼是100"},?
{"PropertyId":19,?"PropertyType":"checkbox",?"PropertyValue":"57|28"}]}?
?????????$.ajax({
????????????type:?"GET",
????????????contentType:?"application/json;?charset=utf-8",
????????????url:?"/WebServices/ProductPropertyWebService.asmx/PostProductPropertyList",
????????????anysc:?false,
????????????data:?{?propertyList:?productPropertyTemplate?},
????????????dataType:?"json",
????????????success:?function?(result)?{?alert(result.d)?},
????????????error:?function?(XMLHttpRequest,?textStatus,?errorThrown)?{
????????????????alert(errorThrown?+?':'?+?textStatus);
????????????}
????????});
?
?
?
服務(wù)器端:
1、要反序列化Json字符為.net對(duì)象,有比較多的開(kāi)源類庫(kù),我使用的是.net 3.5版本以上自帶的DataContractJsonSerializer,寫(xiě)一個(gè)輔助類:
????///?Json序列化和反序列化的幫助方法
????///?</summary>
????public?class?JsonHelper
????{
????????///?<summary>
????????///?JSON序列化:把對(duì)象序列化成Json格式的字符串
????????///?</summary>
????????public?static?string?JsonSerializer<T>(T?t)
????????{
????????????var?ser?=?new?DataContractJsonSerializer(typeof(T));
????????????var?ms?=?new?MemoryStream();
????????????ser.WriteObject(ms,?t);
????????????string?jsonString?=?Encoding.UTF8.GetString(ms.ToArray());
????????????ms.Close();
????????????return?jsonString;
????????}
????????///?<summary>
????????///?JSON反序列化:根據(jù)Json格式的字符串,反序列化成對(duì)象
????????///?</summary>
????????public?static?T?JsonDeserialize<T>(string?jsonString)
????????{
????????????var?ser?=?new?DataContractJsonSerializer(typeof(T));
????????????var?ms?=?new?MemoryStream(Encoding.UTF8.GetBytes(jsonString));
????????????var?obj?=?(T)ser.ReadObject(ms);
????????????return?obj;
????????}
????}?
?
2、因?yàn)橐葱蛄谢上鄳?yīng)的對(duì)象,所以先構(gòu)造兩個(gè)對(duì)象類,注意每個(gè)類和類的字段前面的特性修改符:
????public?class?MProductProperty
????{
????????[DataMember(Order?=?0,?IsRequired?=?true)]
????????public?int?ProductId?{?set;?get;?}
????????[DataMember(Order?=?1,?IsRequired?=?true)]
????????public?List<MProperty>?PropertyList?{?set;?get;?}
????}
????public?class?MProperty
????{
????????[DataMember(Order?=?0,?IsRequired?=?true)]
????????public?int?PropertyId?{?set;?get;?}
????????[DataMember(Order?=?1,?IsRequired?=?true)]
????????public?string?PropertyType?{?set;?get;?}
????????[DataMember(Order?=?2,?IsRequired?=?true)]
????????public?string?PropertyValue?{?set;?get;?}
????}
?
?3、接收并處理Json數(shù)據(jù)的Web方法:
????????[ScriptMethod(UseHttpGet?=?true)]
????????public?string?PostProductPropertyList()
????????{
????????????string?jsonString?=?HttpContext.Current.Request["propertyList"];
????????????var?productProperty?=?JsonHelper.JsonDeserialize<MProductProperty>(jsonString);?//?productProperty?成功反序列化成MProductProperty的對(duì)象
????????????//返回接收成功標(biāo)識(shí)
????????????return?"postsuccess";
????????}
?
?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangjunwl/archive/2011/02/17/1957256.html
總結(jié)
以上是生活随笔為你收集整理的jQuery 结合 Json 提交数据到Webservice,并接收从Webservice返回的Json数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SSM框架搭建(四) springmvc
- 下一篇: 傅里叶光学导论_激光位移传感器的光学系统