JQUERY Ajax将空对象传递给我的控制器
有人能帮我吗 我使用ajax将一些数据对象发送到我的控制器,但我在控制器中得到了对象的空值。我什么都试过了,什么也没发生 我的cs型:JQUERY Ajax将空对象传递给我的控制器,jquery,ajax,asp.net-mvc,controller,Jquery,Ajax,Asp.net Mvc,Controller,有人能帮我吗 我使用ajax将一些数据对象发送到我的控制器,但我在控制器中得到了对象的空值。我什么都试过了,什么也没发生 我的cs型: public class Cliente { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int IdCliente { get; set; } [Required] public string Nome { get; se
public class Cliente
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdCliente { get; set; }
[Required]
public string Nome { get; set; }
public string Sobrenome { get; set; }
public Endereco Endereco { get; set; }
public string Rg { get; set; }
public string Cpf { get; set; }
public Telefone Telefone { get; set; }
//[DataType(DataType.Date)]
public DateTime Nascimento { get; set; }
}
我的javascript:
var cliente = {
nome: $("#nome"),
sobrenome: $("#sobrenome"),
rg: $("#rg"),
cpf: $("#cpf"),
nascimento: $("#nascimento"),
telefone: { numero: $("#telefone") },
endereco: {
logradouro: $("#rua"),
complemento: $("#complemento"),
bairro: $("#bairro"),
cidade: $("#cidade"),
cep: $("#cep"),
}
};
function salva() {
$.ajax({
type: "POST",
async: false,
processData: false,
data: cliente,
//dataType: 'json',
//contentType: 'application/json; charset=utf-8',
url: 'Cadastro',
success: function (msg) {
console.log(msg);
}
});
}
和我的控制器:
[HttpPost]
public ActionResult Cadastro(Cliente cliente)
{
if (service.Validate(cliente))
{
service.Add(cliente);
//return RedirectToAction("Inicio");
return Json(new { Cliente = cliente });
}
return View(cliente);
}
尝试将
cliente
的设置移动到salva
功能中:
function salva() {
var cliente = {
nome: $("#nome"),
sobrenome: $("#sobrenome"),
rg: $("#rg"),
cpf: $("#cpf"),
nascimento: $("#nascimento"),
telefone: { numero: $("#telefone") },
endereco: {
logradouro: $("#rua"),
complemento: $("#complemento"),
bairro: $("#bairro"),
cidade: $("#cidade"),
cep: $("#cep"),
}
};
$.ajax({
type: "POST",
async: false,
processData: false,
data: cliente,
//dataType: 'json',
//contentType: 'application/json; charset=utf-8',
url: 'Cadastro',
success: function (msg) {
console.log(msg);
}
});
}
您是否忘记了所有选择器上的.val()
$("#nome").val();
在通过AJAX调用之前警告数据,以确保您正在获取数据
此外,您还需要执行以下操作:
将您的var客户
更改为var客户对象
,然后尝试以下操作:
contentType: 'application/json',
data: JSON.stringify({ cliente: clienteObj})
我已将内容类型更改为false。这也允许您传递Javascript对象
我还添加了.val()以从该id获取值
哼我认为这不是一个问题。。。无论如何,我尝试放置.val(),但收到一个空对象,我将我的var cliente更改为var clienteObj,它保留了这个错误:Uncaught TypeError:将循环结构转换为JSON看到图片了吗?@ReFarias实际上,你将图片放在了我的答案中,所以我不得不查看它,我拒绝了。您可以在问题中添加该图片。@ReFarias关于您的错误:
uncaughttypeerror:将循环结构转换为JSON
,您不能对日期对象进行JSON编码。来自json.org:“值可以是双引号中的字符串、数字、true或false或null、对象或数组。这些结构可以嵌套。”您可以编辑问题。因此,不要把它放在我的答案中,编辑你的问题
function salva() {
var cliente = {
nome: $("#nome").val(),
sobrenome: $("#sobrenome").val(),
rg: $("#rg").val(),
cpf: $("#cpf").val(),
nascimento: $("#nascimento").val(),
telefone: { numero: $("#telefone").val() },
endereco: {
logradouro: $("#rua").val(),
complemento: $("#complemento").val(),
bairro: $("#bairro").val(),
cidade: $("#cidade").val(),
cep: $("#cep").val(),
}
};
$.ajax({
type: "POST",
async: false,
processData: false,
data: cliente,
cache: false,
contentType: false,
url: 'Cadastro',
success: function (msg) {
console.log(msg);
}
});
}