我有一个Java脚本函数,我想让它将数据传递给我的控制器.
这是我的JavaScript代码:
$.ajax({
type: "POST",
url: "/Home/CreateUser",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({name: 'Dylan', email: 'an@email.com', password: 'password'}),
success: function (response) {
window.location.href = 'dashboard';
},
error: function (error) {
alert('Error while signing up: ' + error);
}
})
..下面是我如何设置我的控制器以接收请求的:
[HttpPost]
public ActionResult CreateUser(string name, string email, string password)
{
Console.WriteLine("Data received successfully: Name - " + name + ", Email - " + email + ", Password - " + password);
return Content("Data received successfully: Name - " + name + ", Email - " + email + ", Password - " + password);
}
目前,每个值都作为null传递,我不明白为什么这不起作用.浏览器网络选项卡中的请求似乎正在发送Json,因此我认为我的控制器设置方式有问题.我试着设置了一个类并接受了它,但仍然不起作用.
谢谢, 迪伦