【问题标题】:How can I RedirectToAction within $.ajax callback?如何在 $.ajax 回调中 RedirectToAction?
【发布时间】:2010-08-20 21:17:34
【问题描述】:

我使用 $.ajax() 每 5 秒轮询一次操作方法,如下所示:

$.ajax({
    type: 'GET', url: '/MyController/IsReady/1',
    dataType: 'json', success: function (xhr_data) {
        if (xhr_data.active == 'pending') {
            setTimeout(function () { ajaxRequest(); }, 5000);                  
        }
    }
});

和 ActionResult 动作:

public ActionResult IsReady(int id)
{
    if(true)
    {
        return RedirectToAction("AnotherAction");
    }
    return Json("pending");
}

我必须将操作返回类型更改为 ActionResult 才能使用 RedirectToAction(最初是 JsonResult,我返回的是 Json(new { active = 'active' };),但它看起来很难从 $.ajax 中重定向和呈现新视图() 成功回调。我需要从这个轮询 ajax 回发中重定向到“AnotherAction”。 Firebug 的响应是来自“AnotherAction”的视图,但它没有呈现。

【问题讨论】:

    标签: asp.net-mvc ajax jquery


    【解决方案1】:

    您需要使用 ajax 请求的结果并使用它来运行 javascript 以自己手动更新 window.location。例如,类似:

    // Your ajax callback:
    function(result) {
        if (result.redirectUrl != null) {
            window.location = result.redirectUrl;
        }
    }
    

    其中“result”是 jQuery 的 ajax 方法在完成 ajax 请求后传递给您的参数。 (要自己生成 URL,请使用 UrlHelper.GenerateUrl,这是一个 MVC 帮助程序,可根据操作/控制器/等创建 URL。)

    【讨论】:

    • 经过详尽搜索后,在另一篇文章中发现了类似的内容。唯一的区别是它改用了 window.location.replace 。谢谢!
    【解决方案2】:

    我知道这是一篇超级旧的文章,但在搜索网络之后,这仍然是 Google 上的最佳答案,我最终使用了不同的解决方案。如果您想使用纯 RedirectToAction 这也可以。 RedirectToAction 响应包含视图的完整标记。

    C#:

    return RedirectToAction("Action", "Controller", new { myRouteValue = foo});
    

    JS:

     $.ajax({
        type: "POST",
        url: "./PostController/PostAction",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        complete: function (result) {
            if (result.responseText) {
                $('body').html(result.responseText);
            }
        }
    });
    

    【讨论】:

      【解决方案3】:

      C# 运行良好

      我刚刚更改了 JS,因为 responseText 不适合我:

       $.ajax({
              type: "POST",
              url: posturl,
              contentType: false,
              processData: false,
              async: false,
              data: requestjson,
              success: function(result) {
                  if (result) {
                      $('body').html(result);
                  }
              },
      
              error: function (xhr, status, p3, p4){
                  var err = "Error " + " " + status + " " + p3 + " " + p4;
                  if (xhr.responseText && xhr.responseText[0] == "{")
                      err = JSON.parse(xhr.responseText).Message;
                  console.log(err);
              }
          });   
      

      【讨论】:

      • 如果您在请求中添加参数“dataType: 'json'”,您将在结果中获得所需的 responseText 变量。
      【解决方案4】:

      您可以在视图中使用 Html.RenderAction 助手:

      public ActionResult IsReady(int id)
      {
          if(true)
          {
              ViewBag.Action = "AnotherAction";
              return PartialView("_AjaxRedirect");
          }
          return Json("pending");
      }
      

      并且在“_AjaxRedirect”部分视图中:

      @{
          string action = ViewBag.ActionName;
          Html.RenderAction(action);
      }
      

      参考: https://stackoverflow.com/a/49137153/150342

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        • 2019-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多