小编Tho*_* O.的帖子

使用JSON将用户定义的对象从jQuery传递给ASP.NET Webmethod

我试图从jQuery传递一些简单的JSON到ASP.NET 4.5 Webmethod.它并没有像我想要的那样工作.如果我接受输入作为单独的参数,它可以工作:

[WebMethod]
public static Address GetJSonAddress(string name, string street)
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将它作为一个对象,它不起作用,传入的内容只是null:

[WebMethod]
public static Address GetJSonAddress(Address newAddress)
Run Code Online (Sandbox Code Playgroud)

我尝试过使用DataContractJsonSerializer的Webmethods,Pagemethods,WCF ......没什么.Address类使用Datamember/DataContract进行适当修饰.属性匹配包括案例.

jQuery,我在其中尝试了所有传递数据的方式,包括将它包装在Address对象中......如果我以任何其他方式执行它而不是我的Webmethod没有被调用,我得到错误500:

Save2 = function () {
var address = { prefix: GLOBALS.curr_prefix };

$('input[id^=' + GLOBALS.curr_prefix + '],select[id^=' + GLOBALS.curr_prefix + ']').each(function () {
       address[this.id.substr(4)] = $.trim($(this).val());
})

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/WebServices/Insert",
    data: JSON.stringify(address),
    dataType: "json",
    success: function (data, textStatus) {
        console.log(data, textStatus);
    },
    failure: function (errMsg) {
        MsgDialog(errMsg);
    }
});
}
Run Code Online (Sandbox Code Playgroud)

最终我将不得不使用121个输入字符串执行此操作,并且实际上不希望有一个包含121个参数的方法.任何帮助表示赞赏.

asp.net jquery json webmethod

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net ×1

jquery ×1

json ×1

webmethod ×1