2013-03-26 35 views
0

我未滿去,並試圖瞭解jQuery的是如何使用asp.net。如果我想用我自己的類在WCF服務功能響應我不能從中得到答案......所以,我在做什麼jQuery的asp.net WCF服務器答案格式

WCF服務:

[ServiceContract(SessionMode = SessionMode.NotAllowed)] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall)] 
    public class Service2 
    { 
     [DataContract] 
     public class Message 
     { 
      [DataMember] 
      public string MessageBody 
      { 
       get; 
       set; 
      } 
      [DataMember] 
      public string Sender 
      { 

       get; 
       set; 

      } 

     } 

     static List<Message> list = new List<Message>(); 


     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
     public List<Message> newone(string a, string b) 
     { 
      list.Add(new Message {MessageBody = a, Sender= b}); 
      return list; 
     } 

    } 

並嘗試從該得到的答案功能從jquery

function hi() { 

       $.ajax({ 
        async: true, 
        url: '/Service2.svc/newone', 
        data: 'a= ' + $('#my1').val() + '&b= ' + $('#my2').val(), 
        type: 'GET', 
        dataType: 'json', 
        success: function (data) { 

         var str = ''; 
         str += data.MessageBody[0];//To take first message from list 


         $("#msgs").html(str); 



        }, 
        error: function() {alert('error');} 

       }); 

      } 

它沒有工作......我必須做什麼從服務器獲得MessageBody和發件人?我怎樣才能在列表中得到第一個元素第二等等?謝謝你的回答!

回答

0

所以,我不明白我的問題的答案,並通過自己找到它...所以不是很複雜,我們必須只是這樣做 - data.d [I] .MessageBody如果想利用消息的列表。