I am trying to pass in some simple JSON to an ASP.NET 4.5 Webmethod from jQuery. And it is not working quite the way I want it. It works if I accept the inputs as separate parameters:
[WebMethod]
public static Address GetJSonAddress(string name, string street)
But if I try to take it as an object it does not work, what gets passed in is simply null:
[WebMethod]
public static Address GetJSonAddress(Address newAddress)
I have tried Webmethods, Pagemethods, WCF using DataContractJsonSerializer...nothing. The Address class is decorated appropriately with Datamember/DataContract. The properties are matched including case.
The jQuery, in which I have tried all manner of passing in the data including wrapping it in an Address object...if I do it any other way than what I have the Webmethod is not called and I get error 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);
}
});
}
Eventually I will have to do this with 121 input strings, and really don't want to have a method with 121 parameters. Any help is appreciated.
data: { newAddress: JSON.stringify(address) }
? – Chicken