日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery 结合 Json 提交数据到Webservice,并接收从Webservice返回的Json数据

發(fā)布時間:2023/12/9 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery 结合 Json 提交数据到Webservice,并接收从Webservice返回的Json数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡單的Json數(shù)據(jù)提交?

jQuery ajax ?webservice:get 和 post

一、GET 方式 客戶端代碼????????????var?data?=?{?classCode:?"0001"};?//?這里要直接使用JOSN對象
????????????$.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);??//?錯誤處理
????????????????}
????????????});?

?

服務(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ù),也要寫成"{}",否則contentType將不能附加在Request?Headers中。
????????????????success:?RenderProperties,
????????????????error:?
function?(XMLHttpRequest,?textStatus,?errorThrown)?{
????????????????????alert(errorThrown?
+?':'?+?textStatus);?//?錯誤處理
????????????????}
????????????});

?

服務(wù)器端 代碼?????[ScriptMethod(ResponseFormat?=?ResponseFormat.Json,?UseHttpGet?=?false)]?//?UseHttpGet?=?false
????????public?List<Property>?GetProductPropertyList(string?classCode,?string?city)?//?Post?方式,參數(shù)對應(yīng)JSON字段屬性,并自動賦值直接使用
????????{
????????????
return?PropertyManager.GetPropertySet(classCode,?"zh-CN").DataList;
????????}

注意:GET方法與POST方法不同,有參數(shù)的時候,如果參數(shù)的值不是ASCII字符(比如中文),GET的參數(shù)要encodeURI編碼,要不服務(wù)端接收到的數(shù)據(jù)為亂碼。

?

?復雜的Json數(shù)據(jù)提交?

?簡單的Json 格式的數(shù)據(jù)如 { name:Yangjun, age:27 }

?復雜的Json格式的數(shù)據(jù),其實只是Json嵌套,比如: {name:yangjun, age:27, child:[{name:yangke, age:1},{name:yangbin, age:2}]}

?如果是這種復雜的Json格式的數(shù)據(jù)要提交,并在Webservices中獲取,然后根據(jù)這個Json格式的字符串,序列成.net對象,應(yīng)該怎么做呢??
比如我要提交下面的數(shù)據(jù):
客戶端:

?

代碼var?productPropertyTemplate?=??{"ProductId":10024,?"PropertyList":[
{
"PropertyId":18,?"PropertyType":"text",?"PropertyValue":"號碼是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對象,有比較多的開源類庫,我使用的是.net 3.5版本以上自帶的DataContractJsonSerializer,寫一個輔助類:

代碼????///?<summary>
????
///?Json序列化和反序列化的幫助方法
????
///?</summary>
????public?class?JsonHelper
????{
????????
///?<summary>
????????
///?JSON序列化:把對象序列化成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格式的字符串,反序列化成對象
????????
///?</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īng)的對象,所以先構(gòu)造兩個對象類,注意每個類和類的字段前面的特性修改符:

代碼????[DataContract]
????
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方法:

代碼????????[WebMethod]
????????[ScriptMethod(UseHttpGet?
=?true)]
????????
public?string?PostProductPropertyList()
????????{
????????????
string?jsonString?=?HttpContext.Current.Request["propertyList"];
????????????var?productProperty?
=?JsonHelper.JsonDeserialize<MProductProperty>(jsonString);?//?productProperty?成功反序列化成MProductProperty的對象
????????????
//返回接收成功標識
????????????return?"postsuccess";
????????}

?

?

?

?

?

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/yangjunwl/archive/2011/02/17/1957256.html

總結(jié)

以上是生活随笔為你收集整理的jQuery 结合 Json 提交数据到Webservice,并接收从Webservice返回的Json数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。