weixin_33724659 2013-11-05 14:40 采纳率: 0%
浏览 55

jQuery Ajax json可序列化

I am learing using jquery ajax to hander the JSON..i writre a demo code. HTMLCODE

$(function () {
            $("#add").click(function () {
                var json = '{ "str":[{"Role_ID":"2","Customer_ID":"155","Brands":"Chloe;","Country_ID":"96;"}]}';
                $.ajax({

                    url: "func.aspx/GetJson",
                    type: "POST",
                    contentType: "application/json",
                    dataType: 'json',
                    data: json, 

                    success: function (result) {
                        alert(result);
                    },
                    error: function () {
                        alert("error");
                    }
                });
            });
        });

 <div>
       <input type="button" value="add" id="add" />
    </div>

i got a input and bind a script function to it, now the proble is comeing.. my C# functiong like that.

[WebMethod]
        public static string GetJson(object str)
        {
            return str.ToString();//good for work


        }
        [Serializable]
        public class TestClass
        {
            public TestClass()
            {
            }

            public TestClass(string role_id, string customer_id, string brands, string countryid)
            {
                this.Role_ID = role_id;
                this.Customer_ID = customer_id;
                this.Brands = brands;
                this.Country_ID = countryid;
            }

            public string Role_ID { get; set; }
            public string Customer_ID { get; set; }
            public string Brands { get; set; }
            public string Country_ID { get; set; }
        }

when i user public static string GetJson(object str) everything is so good.~~ no error at all but . when i try to use my own class TestClass. firebug tell me that "Type 'TestClass' is not supported for deserialization of an array." .any body can give me help:XD

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2013-11-05 15:50
    关注

    This is how it looks when I do this using a WCF webservice. Hopefully it can help you. Let me know if you need any further clarification.

    Script:

    var data = {
            emailAddress: emailAddress,
            firstName: firstName,
            lastName: lastName,
            groups: groups
        };
    
        $.ajax({
            type: "POST",
            url: Client.svc/Subscribe",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            processdata: true,
            success: function (result) {
                //do something
            }
        });
    

    Service:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [BasicHttpBindingServiceMetadataExchangeEndpoint]
    public class Client : IClient
    {
        public bool Subscribe(string emailAddress, string firstName, string lastName, string[] groups)
        {
            //do something
            return true;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器