I'm trying to pass some data to a WebMethod in a Code Behind .cs file. The data resembles the object below, where there's an array that can have any number of additional objects.
var data = {
id: "123",
formula: "liquid",
chemicals: [
{
id: "223",
amount: "0.2",
units: "lb/gal",
},
{
id: "363",
amount: "8.8",
units: "g/li",
}
]
};
The ajax method looks like this:
$.ajax({
type: "POST",
url: "index.aspx/SaveData",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
Where I'm struggling is the WebMethod definition to receive the array of objects.
When I simply send over the top level strings id and formula, it works fine. That web method looks predictable:
[WebMethod]
public static string SaveData(string id, string formula)
{
// do cool stuff
}
When I try to include the chemicals array, I get a failure response. I'm not sure how to match it. I've tried string, string[], and a few others. Any thoughts on how to properly receive this data in a WebMethod?