首页 > 解决方案 > 在 JqueryUI 对话框中捕获并显示错误

问题描述

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

这是一些示例代码:

$(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');

    }
);

尽管偶尔会出现一些奇怪的滚动条和其他东西,但它的效果很好。

问题是当视图抛出错误时,我无法弄清楚如何检查它并将其显示在 jqueryui 面板中

因此,如果视图返回 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 什么?- 我还没有做到这一点。

标签: javascriptasp.net-mvc-5jquery-ui-dialog

解决方案


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

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);
    }
}

推荐阅读