我有一个名为LoginController
和HomeController
的控制器。在LoginController
内部,有一个LoginAsync
函数,看起来像这样:
[HttpPost]
public async Task<ActionResult> LoginAsync(string username, string password)
{
...
var apiResponse = JsonConvert.DeserializeObject<APIResponse>(result);
if (!apiResponse.IsSuccessful)
{
return Content(apiResponse.Message);
}
...
return RedirectToAction("Index", "Home", new { area = "Index" });
}
然后在LoginAsync
的{{1}}函数中通过AJAX对其进行调用:
LoginController
无论出于何种原因, <script type="text/javascript">
$(document).ready(function () {
$('#Login').click(function () {
$.ajax({
url: "Login/LoginAsync",
type: "POST",
data: { username: document.getElementById('txtUsername').value, password: document.getElementById('txtPassword').value },
async: true,
success: function (data) {
alert(data);
//window.location.href = "Home";
},
});
});
});
</script>
行都在触发,但是页面没有像我想的那样重定向到主页。真正让我感到困惑的是,Ajax行return RedirectToAction(...)
显示的是首页(我要重定向到的页面)的HTML内容。因此,在我单击按钮触发Ajax之后,它没有跳到主页,而是弹出了主页的HTML并停留在登录页面上。
如何做到alert(data);
重定向到HomeController / Index视图?