【问题标题】:Custom response message ajax beginform asp.net mvc 4自定义响应消息 ajax beginform asp.net mvc 4
【发布时间】:2013-12-27 21:12:14
【问题描述】:

我在 asp.net mvc 4 中使用 ajax.beginform。在我的控制器中,我有一个 try catch 块

catch (Exception ex)
{
     throw new HttpException(500, ex.Message);
}

在我的 onFailure 事件中

function failure(data) {
     $(".progress").hide();
     $.msgGrowl({
        type: 'error',
        title: 'error',
        text: data.statusText
      });
 }

data.statusText 的结果是“内部服务器错误”。在 data.responseText 中,我看到页面的标题标签包含我想要获取的消息。根据本网站上的一些其他建议,我已更改为

var html = data.responseText;
var error = html.find("title");

但随后我收到一条错误消息,提示没有 find 方法。

非常感谢任何帮助。

【问题讨论】:

  • 你可以试试$(html).find("title")

标签: jquery ajax asp.net-mvc


【解决方案1】:

data.responseText 中的 html 只是一个字符串。您需要将其设为 jQuery 对象才能使用find()

试试:

var html = $(data.responseText);

【讨论】:

  • 这让我更接近我的需要。
  • 抱歉,我按 Enter 添加换行符,当我在 chrome 工具中更改为 var html = $(data.responseText); var title = html.find("title"); 时,它提交了我的评论,它显示标题为 e.fn.e.init[0],我可以钻取到 prevObject 标题对象 innerhtml 和 innertext 属性具有我需要的信息。当我执行 `var error = $(title).innerhtml;'它显示为未定义。我应该如何获得我想要的东西?
  • find() 方法会给你一个 jQuery 对象。要获取文本/html,您可以根据需要的信息简单地使用$(data.responseText).text() 或$(data.responseText).html()
【解决方案2】:

您的回复很有帮助,但我没有足够的分数将其标记为有用。我确信这不是最好的方法,但它让我得到了我需要的东西。

function failure(data) {
        $(".progress").hide();
        var html = $(data.responseText);
        var title = $(html)[1];
        var error = $(title).text();
        $.msgGrowl({
            type: 'error',
            title: 'error',
            text: error
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多