jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬²¢½ÓÊÕ´ÓWebservice·µ»ØµÄJsonÊý¾Ý
jQuery ajax webservice£ºget ºÍ post
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); // ´íÎó´¦Àí } });
·þÎñÆ÷¶Ë
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] //UseHttpGet = true
public List<Property> GetProductPropertyList()
{
string classCode = HttpContext.Current.Request["classCode"]; // Get ·½Ê½£¬ÒªÔÚ²éѯ×Ö·û´®ÀïµÃµ½²ÎÊýÖµ
return PropertyManager.GetPropertySet(classCode, "zh-CN").DataList;
}
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²ÎÊý²»ÄÜΪ¿Õ""£¬Èç¹û²»´«²ÎÊý£¬Ò²ÒªÐ´³É"{}"£¬·ñÔòcontentType½«²»Äܸ½¼ÓÔÚRequest HeadersÖС£ success: RenderProperties, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown + ':' + textStatus); // ´íÎó´¦Àí } });
·þÎñÆ÷¶Ë
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)] // UseHttpGet = false
public List<Property> GetProductPropertyList(string classCode, string city) // Post ·½Ê½£¬²ÎÊý¶ÔÓ¦JSON×Ö¶ÎÊôÐÔ£¬²¢×Ô¶¯¸³ÖµÖ±½ÓʹÓÃ
{
return PropertyManager.GetPropertySet(classCode, "zh-CN").DataList;
}
×¢Ò⣺GET·½·¨ÓëPOST·½·¨²»Í¬£¬ÓвÎÊýµÄʱºò£¬Èç¹û²ÎÊýµÄÖµ²»ÊÇASCII×Ö·û(±ÈÈçÖÐÎÄ)£¬GETµÄ²ÎÊýÒªencodeURI±àÂ룬Ҫ²»·þÎñ¶Ë½ÓÊÕµ½µÄÊý¾ÝΪÂÒÂë¡£
¸´ÔÓµÄJsonÊý¾ÝÌá½»
¼òµ¥µÄJson ¸ñʽµÄÊý¾ÝÈç { name:Yangjun, age:27 }
¸´ÔÓµÄJson¸ñʽµÄÊý¾Ý£¬ÆäʵֻÊÇJsonǶÌ×£¬±ÈÈ磺 {name:yangjun, age:27, child:[{name:yangke, age:1},{name:yangbin, age:2}]}
Èç¹ûÊÇÕâÖÖ¸´ÔÓµÄJson¸ñʽµÄÊý¾ÝÒªÌá½»£¬²¢ÔÚWebservicesÖлñÈ¡£¬È»ºó¸ù¾ÝÕâ¸öJson¸ñʽµÄ×Ö·û´®£¬ÐòÁгÉ.net¶ÔÏó£¬Ó¦¸ÃÔõô×öÄØ£¿
±ÈÈçÎÒÒªÌá½»ÏÂÃæµÄÊý¾Ý£º
¿Í»§¶Ë£º
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); } });·þÎñÆ÷¶Ë£º
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·´ÐòÁл¯£º¸ù¾Ý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¡¢ÒòΪҪ·´ÐòÁл¯³ÉÏàÓ¦µÄ¶ÔÏó£¬ËùÒÔÏȹ¹ÔìÁ½¸ö¶ÔÏóÀ࣬עÒâÿ¸öÀàºÍÀàµÄ×Ö¶ÎÇ°ÃæµÄÌØÐÔÐ޸ķû£º
[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Êý¾ÝµÄWeb·½·¨£º
[WebMethod] [ScriptMethod(UseHttpGet = true)] public string PostProductPropertyList() { string jsonString = HttpContext.Current.Request["propertyList"]; var productProperty = JsonHelper.JsonDeserialize<MProductProperty>(jsonString); // productProperty ³É¹¦·´ÐòÁл¯³ÉMProductPropertyµÄ¶ÔÏó //·µ»Ø½ÓÊճɹ¦±êʶ return "postsuccess"; }
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ