代码之家  ›  专栏  ›  技术社区  ›  Nick.Mc

捕获并在jqueryui对话框中显示错误

  •  1
  • Nick.Mc  · 技术社区  · 6 年前

    我正在使用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,随便什么?-我还没到那一步。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nick.Mc    6 年前

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

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