This works for me:
Javascript-Client:
function Car (name, model, year) {
this.name = name;
this.model = model;
this.year = year;
}
function Test51 () {
const myCar = new Car("Name", "Model", 1903);
/* add extra wrapper name 'car' - the parameter name of the C# webservice method */
const payload = { car: myCar };
Fetch('Test51', "POST", payload, CallbackTest51);
}
function Test52 () {
const myCars = [ new Car("Name 1", "Model 1", 1993), new Car("Name 2", "Model 2", 1961) ];
/* add extra wrapper name 'cars' - the parameter name of the C# webservice method */
const payload = { cars: myCars };
Fetch('Test52', "POST", payload, CallbackTest52);
}
async function Fetch(method, httpType, payload, callBackFunc) {
var uri = "Service.asmx/" + method;
wait fetch(uri, {
method: httpType,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
}).then(async function (rawResponse) {
switch (rawResponse.status) {
case 200:
const response = await rawResponse.json();
callBackFunc(response);
break;
case 401:
break;
default:
break;
}
}).catch(function (error) {
console.log(error);
});
}
function CallbackTest51 (response) {
console.log(response);
}
function CallbackTest52 (response) {
console.log(response);
}
C#-WebService (Service.asmx)
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Test51(Car car)
{
return "OK";
}
[WebMethod]
public string Test52(List<Car> cars)
{
return "OK";
}
}
[Serializable()]
public class Car
{
public string Name { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}