在 Javascript 中调用 Controller 的 ActionResult
Calling an ActionResult of Controller in Javascript
我正在尝试在 javascript 中调用控制器内的 ActionResult。
我的 AdminController
中有这个 ActionResult。
[HttpPost]
public ActionResult Logout()
{
return RedirectToAction("Login", "Login");
}
这是我在 AdminView 中的 Logout button
。
<a class="navbar-link" id="logout" name="logout" href="#">
ログアウト
</a>
并尝试在 JAVSCRIPT 中创建一个事件。
$("#logout").click(function () {
swal({
title: "ログアウト?",
text: "アカウントからサインアウトしますか?",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willLogout) => {
if (willLogout) {
//swal("Poof! Your imaginary file has been deleted!", {
// icon: "success",
//});
$.ajax({
url: "/Admin/Logout",
type: 'POST',
success: function (result) {
document.location.reload(true);
},
error: function (result) {
}
});
}
});
});
这样做的主要目的是使用控制器将用户重定向到登录页面。
我尝试在 .then(willLogout)
中设置 swal("Poof")...
并且有效。
但是当我使用 ajax 调用 ActionResult 时。没用。
我看到了控制台,好像什么也没有显示。
我不知道我做错了什么。
如何通过 Ajax
调用从 Controller 调用 ActionResult
到 Javascript 文件?
嗯,帮助我的初学者。这是我应对的解决方案。
首先,我了解到每个ActionResult都应该有一个View(如果我没记错的话)。
所以,在我的 AdminView.cshtml
中,我创建了一个隐藏按钮。
<button type="submit" id="submitLogout" name="button" value="logout" hidden>logout</button>
之后,在我的JSFILE
我删除了 ajax。首先是错误的。
$("#logout").click(function () {
swal({
title: "ログアウト?",
text: "アカウントからサインアウトしますか?",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willLogout) => {
if (willLogout) {
$("#submitLogout").click();
}
});
});
javascript中的这个函数是触发.cshtml
中的隐藏按钮和触发Controller中的ActionResult
此外,在我的控制器中。这就是我说的每个ActionResult
都要有一个View。
public ActionResult AdminView(string button)
{
if (button == "logout")
{
FormsAuthentication.SignOut();
Session.RemoveAll();
Session.Abandon();
return RedirectToActionPermanent("Login", "Login");
}
else
{
if (!(Session["UserInfo"] is UserModel))
{
return RedirectToActionPermanent("Login", "Login");
}
else
{
return View();
}
}
}
ActionResult中的参数string button
就是你要触发的name of the button
。如果触发成功,它会将按钮的值传递给参数string button
.
我正在尝试在 javascript 中调用控制器内的 ActionResult。
我的 AdminController
中有这个 ActionResult。
[HttpPost]
public ActionResult Logout()
{
return RedirectToAction("Login", "Login");
}
这是我在 AdminView 中的 Logout button
。
<a class="navbar-link" id="logout" name="logout" href="#">
ログアウト
</a>
并尝试在 JAVSCRIPT 中创建一个事件。
$("#logout").click(function () {
swal({
title: "ログアウト?",
text: "アカウントからサインアウトしますか?",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willLogout) => {
if (willLogout) {
//swal("Poof! Your imaginary file has been deleted!", {
// icon: "success",
//});
$.ajax({
url: "/Admin/Logout",
type: 'POST',
success: function (result) {
document.location.reload(true);
},
error: function (result) {
}
});
}
});
});
这样做的主要目的是使用控制器将用户重定向到登录页面。
我尝试在 .then(willLogout)
中设置 swal("Poof")...
并且有效。
但是当我使用 ajax 调用 ActionResult 时。没用。
我看到了控制台,好像什么也没有显示。
我不知道我做错了什么。
如何通过 Ajax
调用从 Controller 调用 ActionResult
到 Javascript 文件?
嗯,帮助我的初学者。这是我应对的解决方案。
首先,我了解到每个ActionResult都应该有一个View(如果我没记错的话)。
所以,在我的 AdminView.cshtml
中,我创建了一个隐藏按钮。
<button type="submit" id="submitLogout" name="button" value="logout" hidden>logout</button>
之后,在我的JSFILE
我删除了 ajax。首先是错误的。
$("#logout").click(function () {
swal({
title: "ログアウト?",
text: "アカウントからサインアウトしますか?",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willLogout) => {
if (willLogout) {
$("#submitLogout").click();
}
});
});
javascript中的这个函数是触发.cshtml
中的隐藏按钮和触发Controller中的ActionResult
此外,在我的控制器中。这就是我说的每个ActionResult
都要有一个View。
public ActionResult AdminView(string button)
{
if (button == "logout")
{
FormsAuthentication.SignOut();
Session.RemoveAll();
Session.Abandon();
return RedirectToActionPermanent("Login", "Login");
}
else
{
if (!(Session["UserInfo"] is UserModel))
{
return RedirectToActionPermanent("Login", "Login");
}
else
{
return View();
}
}
}
ActionResult中的参数string button
就是你要触发的name of the button
。如果触发成功,它会将按钮的值传递给参数string button
.