Ê×ÏÈÎÒ³ÐÈÏ£¬WebService¼¼ÊõÔç¾Í²»ÊÇÐÂÏÊÍæÒâÁË£¬ÏÂÃæÖ»ÊǶÔÕû¸ö¹ý³ÌµÄ´úÂë×ܽᡣ
ǰ̨ѡÔñjQuery·¢ËÍajaxÇëÇ󣬴úÂëÈçÏ£º
$.ajax({ type: "post", url: "Service.asmx/HelloWorld", contentType: "application/json;charset=utf-8", dataType: "json", success: function (msg) { alert(msg.d); } });
²ÎÊýÖУ¬type±ØÐëÊÇpostÀàÐÍ£¬contentTypeÒ²±ØÐëÊÇapplication/json£¬·ñÔò·¢»ØµÄÊý¾ÝÊÇxml¸ñʽ£¬±àÂë²»ÊDZØÐ룬µ«ÊÇΪÁ˱£Ö¤²»³öÂÒÂ룬»¹ÊǼÓÉÏutf-8£¬dataTypeдÊÇjson¡£
ÊÕµ½µÄÊý¾Ý±£´æÔÚmsg.dÀd¾ÍÊÇmsgµÄÒ»¸öÊôÐÔ£¬Òò´Ë²»ÄÜÓÃmsg.d.ÊôÐÔÃûµÄ·½·¨ÒýÓ÷¢»ØµÄÊý¾Ý£¬ÐèÒªÔö¼ÓÒ»Ðд¦Àí³ÌÐò£º
var data = eval('(' + msg.d + ')');
data²ÅÊÇjson½âÂëºóµÄÄÚÈÝ¡£
È»ºóÊÇ·þÎñ¶Ë´úÂ룬ÔÚÏîÄ¿ÖÐн¨Ò»¸öWeb·þÎñÏĬÈÏÃû³ÆÊÇService.asmx£¬Õâ¸öÎļþÖ»ÓÐÒ»ÐдúÂ룬ÕæÕýµÄ´úÂëÎļþÊÇApp_Code ÀïµÄService.cs£¨Ä¬ÈÏÃû³Æ£©£¬ÔÚÕâ¸öÎļþÖпÉÒÔ¿´µ½Ò»Ðб»×¢Ê͵ģº [System.Web.Script.Services.ScriptService]¡£ÕâÀïÐèҪȥµô×¢ÊÍ£¬ÈÃǰ̨¿ÉÒÔͨ¹ý½Å±¾·ÃÎÊ¡£
Service.csÀïÒѾĬÈÏÌí¼ÓÁËÒ»¸öHelloWorldº¯Êý£¬²¢ÇÒ´øÉÏÁË[WebMethod]±êÇ©¡£
ΪÁ˲âÊÔjsonͨÐÅ£¬ÕâÀï¼òµ¥Éè¼ÆÒ»¸ö±£´æÊý¾ÝµÄclass
using System.Runtime.Serialization; namespace Tools { [DataContract] public class UserInfo { private string _uid; private string _name; private DateTime _time; [DataMember(Order=1)] public string Uid { get { return _uid; } set { _uid = value; } } [DataMember(Order=2)] public string Name { get { return _name; } set { _name = value; } } [DataMember(Order=3)] public DateTime Time { get { return _time; } set { _time = value; } } } }
ΪÁËÈñàÒëÆ÷ÕýÈ·½âÎöDataMember±êÇ©£¬ÐèÒªÒýÓÃSystem.Runtime.Serialization£¬Èç¹ûÌí¼ÓÁËusingÓï¾ä£¬±àÒëÆ÷ÒÀÈ»±¨´í£¬ÄǾÍÔÚÏîÄ¿ÀïÌí¼ÓËüµÄÒýÓá£
½ÓÏÂÀ´»Øµ½Service.csÖбàдjsonÉú³É´úÂë¡£
Order²ÎÊý±íÃ÷¸ÃÏî³öÏÖÔÚJsonÊý¾ÝµÄµÚ¼¸Ï´Ó1¿ªÊ¼±àºÅ¡£
Userinfo info =new Userinfo(); info.Uid="ÎÒÊÇID"; info.Name = "²âÊÔ"; info.Time=DataTime.Now(); DataContractJsonSerializer dc = new DataContractJsonSerializer(info.GetType()); string result; using (MemoryStream ms = new MemoryStream()) { dc.WriteObject(ms, info); result = Encoding.UTF8.GetString(ms.ToArray()); } return result;
ʹÓÃDataContractJsonSerializerÐèÒª°üº¬System.Runtime.Serialization.JsonÒýÓã¬Ê¹ÓÃMemoryStreamÐèÒª°üº¬System.IOÒýÓã¬Ê¹ÓÃEncodingÐèÒª°üº¬System.TextÒýÓá£
ÖÁ´Ë£¬Ç°Ì¨ÒѾ¿ÉÒÔ½ÓÊÜjsonÊý¾Ý£¬µ«ÊÇinfo.TimeµÄ½âÎöºÜÌرð£¬ÊÇÀàËÆ\\/Date(1300074709000+0800)\\/µÄ×Ö·û´®£¬ÐèÒª½øÒ»²½½âÎö¡£
½âÎöµÄÄ¿±êÊÇÈ¡³ö1300074709000
+0800£¬´úÂëÈçÏ£º
È»ºóÔÙÓñê×¼DateÊֶδ¦ÀíÈÕÆÚ¡£tstr = data.Time;
left = tstr.indexOf('(');
tstr = tstr.substring(left + 1, tstr.length-2);
var t = new Date(parseInt(tstr));
ÍƼöÐÅÏ¢
- 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²å¼þ
ÆÀÂÛ