Jquery ASP.NET Web API未获取POST数据
我在客户机(本例中为浏览器)中有一些json对象,如下所示Jquery ASP.NET Web API未获取POST数据,jquery,asp.net-web-api,Jquery,Asp.net Web Api,我在客户机(本例中为浏览器)中有一些json对象,如下所示 objeto = { idSala: idSala, listaEtapas: listaEtapas, listaMacros: listaMacros, listaTI: listaTI, listaTU: listaTU, listaUnidades: listaUnidades, listaTorres: listaTorres, valor: valor, regla: regla, f
objeto = {
idSala: idSala,
listaEtapas: listaEtapas,
listaMacros: listaMacros,
listaTI: listaTI,
listaTU: listaTU,
listaUnidades: listaUnidades,
listaTorres: listaTorres,
valor: valor,
regla: regla,
finicio: finicio,
ffin: ffin,
activo: activo
};
$.post("/api/reglas", objeto).done(function() {
alert("ok");
})
正如您所看到的,我通过jquery post方法将其发送到我自己开发机器上的IISexpress服务器
在C#中,我创建了相应的模型:
public class reglaInsercion
{
int idSala { get; set; }
int[] listaMacros { get; set; }
int[] listaEtapas { get; set; }
int[] listaTI { get; set; }
int[] listaTU { get; set; }
int[] listaUnidades { get; set; }
string [] listaTorres { get; set; }
double valor { get; set; }
string regla { get; set; }
DateTime finicio { get; set; }
DateTime ffin { get; set; }
bool activo { get; set; }
}
我还设置了相应的控制器动作
[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
但是,当我调试代码时,laRegla对象的所有成员都将为null或零,具体取决于数据类型。我错过了什么?我已经阅读了文档,但我找不到我做错了什么。我可能错了,但我认为您需要以下通过AJAX调用的方法
[System.Web.Services.WebMethod]
[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
您需要按如下方式定义模型:
public class reglaInsercion
{
public int idSala { get; set; }
public int[] listaMacros { get; set; }
public int[] listaEtapas { get; set; }
public int[] listaTI { get; set; }
public int[] listaTU { get; set; }
public int[] listaUnidades { get; set; }
public string[] listaTorres { get; set; }
public double valor { get; set; }
public string regla { get; set; }
public DateTime finicio { get; set; }
public DateTime ffin { get; set; }
public bool activo { get; set; }
}
正在向属性添加
public
键。在Chrome控制台中:未捕获引用错误:未定义idSala
。成功了!!!经过四个小时的几乎所有的尝试。谢谢。我现在觉得自己很笨,你是一个受欢迎的朋友,别忘了接受这个答案作为一个有效的回答,它可能对其他用户有用。干杯。:)