I've got a Javascript function, and I want it to pass through data to my controller.
This is my Javascript code:
$.ajax({
type: "POST",
url: "/Home/CreateUser",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({name: 'Dylan', email: '[email protected]', password: 'password'}),
success: function (response) {
window.location.href = 'dashboard';
},
error: function (error) {
alert('Error while signing up: ' + error);
}
})
..and this is how I've got my Controller set up to receive the request:
[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);
}
Currently each value is passed through as null and I can't figure out why this isn't working. The request in the browser network tab appears to be sending the Json so I think there is something wrong with how I have set up my controller. I've tried setting up a class and receiving that, but that still won't work.
Thanks, Dylan
[FromBody]before the parameter's type.