Hey there! I'm currently working on an ASP.NET Core application that utilizes AJAX and a login.js file to send data to my API. However, when trying to execute JSON.stringify(formData) in the browser, I encounter the error message: Uncaught ReferenceError: formData is not defined at :1:16. I've tried several solutions to tackle this issue, but it seems persistent. Any suggestions?
function logar(event) {
event.preventDefault();
var formData = {
email: $("input[name='email']").val(),
password: $("input[name='password']").val()
};
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify(formData),
url: "https://localhost:7291/api/User",
success: function (result) {
// Success logic
},
error: function (error) {
// Error logic
}
});
};
This is the content of my page:
@model SquadManager.Models.UserViewModel
@{
Layout = null;
}
<link href="~/css/login.css" rel="stylesheet" />
<section>
<h1>Authentication</h1>
<form>
<input type="email" id="email" name="email" value="@Model.Email" placeholder="Username/e-mail" />
<input type="password" id="password" name="password" value="@Model.Password" placeholder="Password" />
<a href="#">Forgot your password?</a>
<button type="submit" onclick="logar(event);">Login</button>
</form>
<label>Don't have an account? <a href="#">Create your account here!</a> </label>
</section>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/login.js"></script>
Check out the console error screenshot for more details on the result: Console Error