javascript - 在 JqueryUI 对话框中捕获和显示错误

标签 javascript asp.net-mvc-5 jquery-ui-dialog

我正在使用 JQuery-UI 对话框加载 MVC 分部 View

下面是一些示例代码:

$(function () {

function fSetupDialogs() {

    $("#dialog-popup").dialog({
      autoOpen: false,
      resizable: true,
      modal: true
    });
}
});



$('body').on('click', '.popup-Edit',
    function (e) {

        url = $(this).data("url");

        $("#dialog-popup")
            .load(url)
            .html("<img src='/content/images/Spinner.gif'>")
            .dialog("option", "title", "Edit " + url.split("/").pop())
            .dialog('open');

    }
);

尽管偶尔会出现一些奇怪的滚动条之类的东西,但效果还是不错的。

问题是当 View 抛出错误时,我不知道如何检查它并在 jqueryui 面板中显示它

因此,如果 View 返回 500 或 401 错误,我想捕获它并在对话框中显示它。现在发生的是微调 GIF 永远坐在那里。如果我打开 F12 控制台,我可以在其中看到错误。

我已经尝试使用 .error 事件处理程序,它确实捕获它并弹出一个消息框。但我想在弹出窗口中显示:

        // This pops up an error alert
        $("#dialog-popup")
            .load(url)
            .error(alert("error"))
            .html("<img src='/content/images/Spinner.gif'>")
            .dialog("option", "title", "Edit " + url.split("/").pop())
            .dialog('open');

如何在对话框中显示内容?这没有效果 - 微调器留在那儿

        // This has no effect - I want to see the words "error" 
        $("#dialog-popup")
            .load(url)
            .error($("#dialog-popup").html("error"))
            .html("<img src='/content/images/Spinner.gif'>")
            .dialog("option", "title", "Edit " + url.split("/").pop())
            .dialog('open');

对于奖励积分,我如何使用javascript错误对象来识别401、500什么的? - 我还没有做到这一点。

最佳答案

我查看了自己的代码并找到了答案。

load 方法接受一个回调参数,可用于执行我需要的操作

我编写了一个名为 popupComplete 的函数,它会在加载完成时调用。

// This loads a partial MVC view into the popup
$("#dialog-popup")
    .load(url,popupComplete)
    .html("<div align='middle'><img src='/content/images/Spinner.gif'></div>")
    .dialog("option", "title", "New")
    .dialog("open");

// when the load is complete, this is called
// which optionally displays an error inside the popup
function popupComplete(response, status, xhr) {
    if (status === "error") {
        var msg =
            "<BR><BR><BR><H3 style='text-align: center;'><span style='color: red;' class='glyphicon glyphicon-fire'></span>   Confound it!<BR><BR>" +
            xhr.status + " " + xhr.statusText + 
            "<BR><BR>" + (xhr.status == 401 ? "Try logging in again" : "") +
            "</B></H3>";
        $("#dialog-popup").html(msg);
    }
}

关于javascript - 在 JqueryUI 对话框中捕获和显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408394/

相关文章:

javascript - 单选按钮(如果单击)

c# - 日期的 MVC 模型验证

c# - 身份 2.0 用户身份在 MVC 5 Controller 初始化程序中为空

jquery-ui - 模态 ui 对话框内的 jquery UI 自动完成 - 建议未显示?

jquery - Haacked JQuery Undoable "Object doesn' t 支持此属性”

javascript - 更改页面的 URL 参数

javascript - 下拉列表值的更改不会将更新后的值返回到 Controller

asp.net - $ ("#dialog").parent().appendTo($ ("form:first"));

jquery-ui-dialog - jQuery UI 对话框 - 通过实例方法检查是否存在

javascript - 使用 Javascript Safari 加载 CSS