【问题标题】:Jquery Ajax json SerializableJquery Ajax json 可序列化
【发布时间】:2013-11-16 11:29:54
【问题描述】:

我正在学习使用 jquery ajax 来处理 JSON..我编写了一个演示代码。 HTML代码

$(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>

我得到了一个输入并绑定了一个脚本函数,现在问题来了.. 我的 C# 函数就是这样。

[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; }
        }

当我使用 公共静态字符串 GetJson(object str) 一切都很好。~~完全没有错误 但 。当我尝试使用我自己的类TestClass。 萤火虫告诉我 “数组的反序列化不支持类型‘TestClass’。” .任何机构都可以给我帮助:XD

【问题讨论】:

    标签: c# javascript jquery ajax json


    【解决方案1】:

    这是我使用 WCF Web 服务执行此操作时的外观。希望它可以帮助你。如果您需要任何进一步的说明,请告诉我。

    脚本:

    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
            }
        });
    

    服务:

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

    【讨论】:

      猜你喜欢
      • 2023-03-02
      • 2015-12-21
      • 2012-12-07
      • 2011-06-30
      • 2011-12-05
      • 2010-12-29
      • 2015-04-10
      • 1970-01-01
      • 2011-04-07
      相关资源
      最近更新 更多