【问题标题】:Server side redirection after jquery ajax call in asp.net mvc 4在asp.net mvc 4中jquery ajax调用后的服务器端重定向
【发布时间】:2013-11-05 17:33:20
【问题描述】:

我正在将登录信息从 jQuery AJAX 调用发送到 MVC 4 控制器:

   $.post(url, data, function (response) {
      if (response=='InvalidLogin') {
          //show invalid login
      }
      else if (response == 'Error') {
          //show error
      }
      else {
          //redirecting to main page from here for the time being.
          window.location.replace("http://localhost:1378/Dashboard/Index");
      }
   });

如果登录成功,我想根据用户类型将用户从服务器端重定向到适当的页面。如果登录失败,则返回一个字符串给用户:

    [HttpPost]
    public ActionResult Index(LoginModel loginData)
    {
        if (login fails)
        {
            return Json("InvalidLogin", JsonRequestBehavior.AllowGet);
        }
        else
        {
             // I want to redirect to another controller, view, or action depending
             // on user type.
        }
    }

但是有问题:

  1. 如果此方法返回“ActionResult”,则我收到错误 not all code paths return a value

  2. 如果我使用 'void',我不能返回任何东西。

  3. 即使我使用没有返回的“void”,由于 jQuery AJAX 调用的异步特性,我也无法重定向到其他控制器或视图。

有什么技术可以处理这种情况吗?

【问题讨论】:

  • 您是否考虑过尝试在 ActionResult 的 else 中返回一些东西?
  • 在您的else 中返回“成功”。
  • @Jasen,你没有仔细阅读问题。
  • 怎么样?从您的控制器返回一些内容并在 javascript 中捕获响应解决了问题 1 和 2。您接受的答案本质上是相同的。
  • @Jasen,如果您有比接受的答案更好的东西,请告诉我们。

标签: jquery ajax asp.net-mvc-4


【解决方案1】:

return 通常从方法返回而不在其中执行任何进一步的语句,因此不需要else 部分。这样您就可以解决问题 #1。

至于重定向为什么不返回某种redirect命令:

[HttpPost]
public ActionResult Index(LoginModel loginData)
{
    if (login fails)
    {
        return Json(new {result = "InvalidLogin"}, JsonRequestBehavior.AllowGet);
    }
    return Json(new {result = "Redirect", url = Url.Action("MyAction", "MyController")});
}

然后在javascript中:

$.post(url, data, function (response) {
  if (response.result == 'InvalidLogin') {
      //show invalid login
  }
  else if (response.result == 'Error') {
      //show error
  }
  else if (response.result == 'Redirect'){
      //redirecting to main page from here for the time being.
      window.location = response.url;
  }
 });

【讨论】:

  • okey :D,我怎样才能用同样的方法从 js/server 端重定向到带有模型的视图?
【解决方案2】:

这对我有帮助。

return JavaScript("window.location = '/'");

参考。关联 How to get an ASP.NET MVC Ajax response to redirect to new page...

【讨论】:

    【解决方案3】:

    我不得不这样做,但我发现的解决方案都没有真正满足我对 JavaScript 解析错误的极端要求,为了将客户端重定向到登录页面,我必须避免这些错误。

    我所做的是从我的自定义授权属性发送一个简单的“NOAUTH”字符串响应,然后在任何事件处理程序被命中之前拦截 Ajax 响应,并通过设置 window.location 重定向用户。

    服务器端:

    protected override void HandleUnauthorizedRequest(AuthorizationContext context)
    {
        if (context.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            var result = new ContentResult {Content = "NOAUTH"};
            context.Result = result;
            return;
        }
    }
    

    然后在客户端:

    $.ajaxSetup({
        dataFilter: function (data, type) {
            if (data !== "" && data === "NOAUTH") {
                window.location = '/';
            }
            return data;
        }
    });
    

    不过,我不推荐这种方法。如果您必须这样做,那么我至少建议将“NOAUTH”值放在 http 响应标头中,然后在全局 JQuery 完整处理程序中读取该值。

    服务器端:

    HttpContext.Current.Response.AddHeader("NOAUTH", "1");
    

    客户端:

    $.ajaxSetup({
        complete: function (jqXHR, textStatus) {
            if (jqXHR.getResponseHeader("NOAUTH") === '1')
                window.location = '/';
        }
    });
    

    请注意,使用 dataFilter 是在您的任何其他事件处理程序被命中之前拦截 ajax 请求的唯一方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多