【问题标题】:ASP.Net MVC 3 JsonResult redirect errorASP.Net MVC 3 JsonResult 重定向错误
【发布时间】:2012-04-25 17:39:25
【问题描述】:

我有一个 ASP.Net MVC 3 应用程序,它使用 jQuery .ajax 调用来 POST 到服务器端控制器操作,如下所示

客户端 jQuery 调用:

//Page the server
        $.ajax({
                url: '/Home/Call_Abort',
                type: 'POST',
                data: "{ 'op_id': '" + ajaxOPID + "', 'statMsg': '" + ajaxStatMsg + "'}",
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                        window.location.href = data.redirectUrl;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                        alert("Error while paging the server to abort.  Reported error: '" + xhr.responseText + "'.");
                }
        });

服务器-控制器操作:

[HttpPost]
public JsonResult Call_Abort(string op_id, string statMsg)
{
    return Json(new
    {
        redirectUrl = Url.Action("Operator_Home", "Home", new { op_id = op_id, status = statMsg }),
        isRedirect = true
    });

}

返回 URL 应该将用户重定向到不同的视图(即 Operator_Home 视图)。这适用于我的本地开发 PC,按预期路由到 Operator_Home 视图,但是当我在我的开发 Web 服务器(带有 IIS 7 的服务器 2008)上运行它进行测试时,我收到以下 404 错误页面作为 xhr .responseText 导致上面的 .ajax 调用。

似乎发生的事情不是重定向到我在 redirectURL 中指定的视图(即 Operator_Home),它似乎认为 Call_Abort 控制器操作应该返回一个 Call_Abort 视图,因为没有这样的视图存在下面的错误被抛出。但是为什么这会发生在 Web 服务器上,而不是我运行 Visual Studio 开发服务器的本地 PC 上呢?是否有一些设置我需要为我在 IIS 上的应用程序进行调整,以便让它像在我的开发机器上一样运行。我对 MVC 路由的理解不够清楚,无法知道为什么会发生这种情况。感谢您提供任何帮助或见解。

更新

抱歉,我在工作地点使用了几台服务器,我引用了错误的 Web 服务器。我运行它的服务器是带有 IIS7 的 Server 2008

【问题讨论】:

  • 我删除了我的答案,因为我绝对没有解决它的根本问题。我实际上没有将 IIS6 与 MVC 一起使用,所以我无法回答我认为的根本原因。
  • 谢谢 Andrew,我实际上犯了一个错误,我的工作中有几台服务器,我引用了错误的服务器。具体的 Web 服务器是运行 IIS 7 的 Server 2008。
  • 如果您有一个应用程序错误断点,您能否在附加到服务器后发布您在服务器上遇到的实际错误?

标签: asp.net-mvc asp.net-mvc-3


【解决方案1】:

错误原来是 .Ajax 调用中的 URL 有问题我修改了 URL,如下所示:

//Page the server
var abortURL = window.location.origin + '/myApp/Home/Call_Abort';
        $.ajax({
                url: abortURL,
                type: 'POST',
                data: "{ 'op_id': '" + ajaxOPID + "', 'statMsg': '" + ajaxStatMsg + "'}",
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                        window.location.href = data.redirectUrl;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                        alert("Error while paging the server to abort.  Reported error: '" + xhr.responseText + "'.");
                }
        });

这解决了问题并允许 jQuery 完成 POST。

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    相关资源
    最近更新 更多