【问题标题】:Throw an error back to jQuery from an MVC HttpPost从 MVC HttpPost 向 jQuery 抛出错误
【发布时间】:2012-10-09 14:30:22
【问题描述】:

我在控制器中有一个方法,如下所示:

[HttpPost]
public void UnfavoriteEvent(int id)
{
    try
    {
        var rows = _connection.Execute("DELETE UserEvent WHERE UserID = (SELECT up.UserID FROM UserProfile up WHERE up.UserName = @UserName) AND EventID = @EventID",
            new { EventID = id, UserName = User.Identity.Name });
        if (rows != 1)
        {
            Response.StatusCode = 500;
            Response.Status = "There was an unknown error updating the database.";
            //throw new HttpException(500, "There was an unknown error updating the database.");
        }
    }
    catch (Exception ex)
    {
        Response.StatusCode = 500;
        Response.Status = ex.Message;
        //throw new HttpException(500, ex.Message);
    }
}

如您所见,我尝试了几种不同的方法来解决这个错误。在 JavaScript 中,我有以下代码块来调用此方法:

var jqXHR;
if (isFavorite) {
    jqXHR = $.ajax({
        type: 'POST',
        url: '/Account/UnfavoriteEvent',
        data: { id: $("#EventID").val() }
    });
}
else {
    jqXHR = $.ajax({
        type: 'POST',
        url: '/Account/FavoriteEvent',
        data: { id: $("#EventID").val() }
    });
}

jqXHR.error = function (data) {
    $("#ajaxErrorMessage").val(data);
    $("#ajaxError").toggle(2000);
};

现在,我要做的是将发生的错误返回到jqXHR.error 函数,以便我可以正确处理它。

目前未注释的代码会引发异常,指出我在 Status 中放置的文本是不允许的,而注释代码实际上返回标准错误页面作为响应(这并不奇怪)。

所以,我有几个问题:

  1. 如何正确抛出错误?
  2. Response.Status 属性有什么作用?

谢谢大家!

【问题讨论】:

  • 由于您正在捕获一般异常,因此我不会将特定消息发送回客户端。这最终可能成为有关您的应用程序的敏感信息。如果您在控制消息的情况下捕获自定义域异常,则可以将它们发送给客户端,但对于一般异常,您应该发送一般消息。
  • @BZink,非常好的观点。谢谢。

标签: c# jquery .net error-handling asp.net-mvc-4


【解决方案1】:

您将能够从 javascript 端获取响应状态,执行以下操作:

$.ajax({
    type: 'POST',
    url: '/Account/UnfavoriteEvent',
    data: { id: $("#EventID").val() },
    success: function(data, textStatus, jqXHR) {
        // jqXHR.status contains the Response.Status set on the server
    },
    error: function(jqXHR, textStatus, errorThrown) {
        // jqXHR.status contains the Response.Status set on the server
    }});

如您所见,您必须将error 的函数传递给ajax 函数...在您的示例中,您将函数设置为jqXHRerror 属性,完全没有效果.

有关 ajax 事件的文档

jQuery 文档说错误字符串将出现在 errorThrown 参数中。

不要使用响应

您应该返回HttpStatusCodeResult

[HttpPost]
public void UnfavoriteEvent(int id)
{
    try
    {
        var rows = _connection.Execute("DELETE UserEvent WHERE UserID = (SELECT up.UserID FROM UserProfile up WHERE up.UserName = @UserName) AND EventID = @EventID",
            new { EventID = id, UserName = User.Identity.Name });
        if (rows != 1)
        {
            return new HttpStatusCodeResult(500, "There was an unknown error updating the database.");
        }
    }
    catch (Exception ex)
    {
        return new HttpStatusCodeResult(500, ex.Message);
    }
}

【讨论】:

  • 这肯定让我进入了 JavaScript 中的错误方法,所以谢谢!但我想要的错误字符串不在Response.Status 中。我在服务器端做错了什么,如何将简单的消息传递给客户端?
  • 谢谢,这正是我所需要的!
【解决方案2】:

使用Response.StatusDescription。在 jQuery 端,使用jqXHR.fail(function(){})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多