javascript - 在 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 什么?- 我还没有做到这一点。
解决方案
我查看了自己的代码并找到了答案。
该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);
}
}
推荐阅读
- azure-logic-apps - Azure Logic Apps EDIFACT DECODE Allow underscore(_) character in
- ruby - Is there a way to include ticks/gridlines on non sequential axis? Also, is it possible to add border styling and custom tick positions?
- forms - 如何解决此错误:Joomla\CMS\Form\Form::getInstance() 无法加载文件
- python - 从具有重复键的字典列表中创建具有唯一键的字典列表
- java - 如何创建地图
> 有更多的条件? - excel - 将一系列单元格剪切并粘贴到选定的目标位置
- docker - 我需要在谷歌云平台上使用 containerd 吗?
- javascript - 我正在尝试动态添加多个 tagit 字段,但它不适合我
- javascript - 卸载后未清除 React setTimeout 或组件更新
- python - 如何使用 Python 计算数组特定行中的值