0

我正在尝试了解 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 和 Sender?我如何获得第二个列表中的第一个元素等等?感谢您的回答!

4

1 回答 1

0

所以,我没有得到我的问题的答案并自己找到它......所以它不是很复杂,我们必须这样做 - data.d[i].MessageBody 如果想从列表中获取消息。

于 2013-03-26T18:08:47.980 回答