Incorporating Ajax into my MVC application allows me to initiate a POST request when the user clicks on the logout button. The goal is to redirect the user back to the Login page and route them to the HttpPost method in the controller. However, I encounter an error when attempting to log out using Ajax:
This localhost page can’t be found No webpage was found for the web address: http://localhost/myApp/Error/Error?code=22
PageHeader.cshtml
...
items.Add()
.Widget(w => w
.Button()
.Type(ButtonType.Normal)
.StylingMode(ButtonStylingMode.Text)
.Text("Logout")
.ID("logoutbutton")
.OnClick("logoutOnClick")
)
.CssClass("toolbar-button")
.LocateInMenu(ToolbarItemLocateInMenuMode.Auto)
.Location(ToolbarItemLocation.After);
})
)
...
<script type="text/javascript">
function logoutOnClick () {
if (confirm('Are you sure you want to logout?')) {
$.ajax({
type: "POST",
url: '@Url.Action("Login", "Account")'
});
}
}
</script>
AccountController.cs
...
[HttpGet]
public IActionResult Login() { return View(new LoginModel()); }
[HttpPost, ValidateAntiForgeryToken]
public IActionResult Login(LoginModel userModel)
{
bool? isAuthenticated = false;
...