my ajax post to controller does not work. The code below is a basic example of what I am trying to do.
On debug mode, it returns null and the values returned to the ajax call is empty, even if I appened the attribute
public IActionResult Login([FromBody]Data data)
It still does not work
Here is my ajax all
$('#SignIn').click(function () {
$('#SignIn').val('Please Wait');
Ajax_Login();
});
function Ajax_Login() {
var data = {
username: $('#Email').val().trim(),
password: $('#Password').val()
};
$.ajax({
type: "POST",
url: "/Upload/Login",
data: data,
contentType: "application/json",
dataType: "json",
success: function (message) {
alert(message);
},
error: function (message) {
alert(message);
}
I have also tried using data: JSON.stringfy({ User: Username, Pass: Password })
Here is my controller
public class Data
{
public string Username { get; set; }
public string Password { get; set; }
}
Controller method
[HttpPost]
public IActionResult Login(Data data)
{
var message = data.Username + " and " + data.Password;
return Json(message);
}
contentType: "application/json",ajax option.contentType: "application/json"in mvc if you have generated your view correctly. But if you did have that option, then you would also have neededdata: JSON.stringify(data),var data = { ... };code - it would be justdata = $('form').serialize();