欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery結(jié)合Json提交數(shù)據(jù)到Webservice,并接收從Webservice返回的Json數(shù)據(jù)

 更新時間:2011年02月18日 01:02:39   作者:  
簡單的Json數(shù)據(jù)提交,后臺結(jié)合asp.net,需要的朋友可以參考下。
jQuery ajax webservice:get 和 post
一、GET 方式
客戶端
復(fù)制代碼 代碼如下:

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ù)器端
代碼
復(fù)制代碼 代碼如下:

[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 方式
客戶端
代碼
復(fù)制代碼 代碼如下:

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ù)器端
代碼
復(fù)制代碼 代碼如下:

[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ù)為亂碼。
復(fù)雜的Json數(shù)據(jù)提交
簡單的Json 格式的數(shù)據(jù)如 { name:Yangjun, age:27 }
復(fù)雜的Json格式的數(shù)據(jù),其實只是Json嵌套,比如: {name:yangjun, age:27, child:[{name:yangke, age:1},{name:yangbin, age:2}]}
如果是這種復(fù)雜的Json格式的數(shù)據(jù)要提交,并在Webservices中獲取,然后根據(jù)這個Json格式的字符串,序列成.net對象,應(yīng)該怎么做呢?
比如我要提交下面的數(shù)據(jù):
客戶端:
代碼
復(fù)制代碼 代碼如下:

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,寫一個輔助類:
代碼
復(fù)制代碼 代碼如下:

/// <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)造兩個對象類,注意每個類和類的字段前面的特性修改符:
代碼
復(fù)制代碼 代碼如下:

[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方法:
代碼
復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • jQuery實現(xiàn)的圖片輪播效果完整示例

    jQuery實現(xiàn)的圖片輪播效果完整示例

    這篇文章主要介紹了jQuery實現(xiàn)的圖片輪播效果,結(jié)合完整實例形式分析了jQuery結(jié)合時間函數(shù)動態(tài)改變頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下
    2016-09-09
  • jQuery控制TR顯示隱藏的幾種方法

    jQuery控制TR顯示隱藏的幾種方法

    使用id,這個方法可以在生成html的時候動態(tài)設(shè)置tr的id,也是用得最多最簡單的一種,其他方法祥看本文
    2014-06-06
  • JQuery 風(fēng)格的HTML文本轉(zhuǎn)義

    JQuery 風(fēng)格的HTML文本轉(zhuǎn)義

    astinus開發(fā)過程中,我自己就在不斷的使用。有次貼了一些JS代碼進(jìn)去,于是頁面顯示錯誤。顯然,把源代碼直接放進(jìn)html文本了——好吧,從05年轉(zhuǎn)去做網(wǎng)游以后,一直沒有正經(jīng)的做過web了。
    2009-07-07
  • JQuery獲取表格數(shù)據(jù)示例代碼

    JQuery獲取表格數(shù)據(jù)示例代碼

    這篇文章主要介紹了通過JQuery如何獲取表格數(shù)據(jù),下面有個不錯的示例,大家可以參考下
    2014-05-05
  • easyUI combobox實現(xiàn)聯(lián)動效果

    easyUI combobox實現(xiàn)聯(lián)動效果

    這篇文章主要為大家詳細(xì)介紹了easyUI combobox實現(xiàn)聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • jQuery中ajax的相關(guān)知識點匯總

    jQuery中ajax的相關(guān)知識點匯總

    這篇文章主要給大家介紹了關(guān)于jQuery中ajax相關(guān)知識點的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • jQuery zTree樹插件簡單使用教程

    jQuery zTree樹插件簡單使用教程

    這篇文章主要為大家詳細(xì)介紹了jQuery zTree樹插件簡單使用教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • jquery blockUI 遮罩不能消失與不能提交的解決方法

    jquery blockUI 遮罩不能消失與不能提交的解決方法

    jquery blockUI 遮罩不能消失與不能提交的解決方法,使用jquery blockUI的朋友可以參考下。
    2011-09-09
  • Jquery加載時從后臺讀取數(shù)據(jù)綁定到dropdownList實例

    Jquery加載時從后臺讀取數(shù)據(jù)綁定到dropdownList實例

    從后臺讀取數(shù)據(jù)綁定到dropdownList,option選項value動態(tài)賦值,具體實現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • jQuery插件FusionCharts實現(xiàn)的Marimekko圖效果示例【附demo源碼】

    jQuery插件FusionCharts實現(xiàn)的Marimekko圖效果示例【附demo源碼】

    這篇文章主要介紹了jQuery插件FusionCharts實現(xiàn)的Marimekko圖效果,結(jié)合實例形式分析了jQuery使用FusionCharts插件結(jié)合xml數(shù)據(jù)繪制Marimekko圖的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2017-03-03

最新評論