【发布时间】:2018-07-27 22:00:17
【问题描述】:
我正在通过 jquery ajax post 调用 MVC 操作方法。此操作方法将返回一个字符串值,或者如果操作方法中的某些条件为真,它将重定向到另一个操作方法。问题是每当我试图重定向到另一个动作方法时,ajax post 正在执行错误块,并且每当动作方法返回一个字符串值时它工作正常。我不想使用 html 表单发布。以下是我的代码:
function VerifyCreds() {
$.ajax({
type: "POST",
url: "/Login/Verify",
data: '{Username: "' + $("#txtUsername").val() + '",Password: "' + $("#txtPassword").val() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
public ActionResult Verify(string Username, string Password)
{
string Response = "0";
EmployeeEntities ent = new EmployeeEntities();
var val = ent.VerifyUser(Username, Password);
Response = val.FirstOrDefault().ToString();
if (Response == "1")
{
return this.RedirectToAction("LandingPage");
}
return Content(Response);
}
public ActionResult LandingPage()
{
return View();
}
【问题讨论】:
标签: javascript jquery asp.net-mvc