【问题标题】:Controller Action method return string or Redirect to another action with ajax post request控制器操作方法返回字符串或使用 ajax 发布请求重定向到另一个操作
【发布时间】: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


    【解决方案1】:

    您明确指定 json 作为数据类型。这意味着,您的 ajax 方法期望来自服务器的响应为 json 字符串。 jQuery ajax 方法将使用 JSON.parse 方法从这个 json 字符串创建一个 JavaScript 对象并将其传递给回调。当您从服务器进行重定向时,它不会返回 200 OK 响应,而是返回 302 重定向响应,并且浏览器通常会重新调用响应的 location 标头值。您将收到 ajax 方法的错误回调,因为服务器正在返回一些无法使用 JSON.parse 安全地转换为 JavaScript 对象的字符串。 (我的猜测是 LandingPage 调用的响应!。检查网络选项卡以查看即将出现的响应)

    如果你绝对想处理这种情况,你最好在这两种情况下都返回一个正确的 JSON 结构,并在客户端检查这个 json 数据并根据需要进行重定向。

    我还建议您使用适当的类型。使用int 而不是将数字存储在string 变量中。

    public ActionResult Verify(string Username, string Password)
    {       
        var ent = new EmployeeEntities();
        var val = ent.VerifyUser(Username, Password);
        var response = val.FirstOrDefault();
        if (response == 1)
        {
           return Json(new { status="redirect", url = Url.Action("LandingPage")});      
        }
        return Json(new { status="success", message = "Valid login" });
    }
    

    在您的 ajax 调用成功事件中,检查此 json 数据并基于 status 属性,执行重定向或向用户显示消息。

    success: function (response) {
                if(response.status==="redirect")
                {
                    window.location.href = response.url;
                }
                else
                {
                    alert(response.message);
                }
            },
    

    您也可以从您的 ajax 调用中完全删除 dataType: "json",。 jQuery 会根据响应头智能地猜测类型并使用它。由于我们从 action 方法中显式返回 Json,因此响应 Content-Type 标头值将是 application/json

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多