首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery Ajax json Serializable

Jquery Ajax json Serializable
EN

Stack Overflow用户
提问于 2013-11-05 22:40:51
回答 1查看 394关注 0票数 0

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

代码语言:javascript
复制
$(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#函数就是这样的。

代码语言:javascript
复制
[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时。firebug告诉我“数组的反序列化不支持类型'TestClass‘。”firebug body可以提供帮助:XD

EN

回答 1

Stack Overflow用户

发布于 2013-11-05 23:50:23

这就是我使用WCF WCF服务来做这件事时的样子。希望它能帮助你。如果你需要进一步的澄清,请告诉我。

脚本:

代码语言:javascript
复制
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
        }
    });

服务:

代码语言:javascript
复制
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[BasicHttpBindingServiceMetadataExchangeEndpoint]
public class Client : IClient
{
    public bool Subscribe(string emailAddress, string firstName, string lastName, string[] groups)
    {
        //do something
        return true;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19791566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档