如何在 $.ajax() error() 上显示 BlockUI 模式对话框?
当我单击表单的提交按钮时,这会调用 startValidateFormParameters()
函数。
在此函数中,我使用 BlockUI 阻止 UI,并运行 AJAX 请求来验证表单中的某些信息。
如果表单数据在某些方面有问题,AJAX url
将返回 403 错误响应,从而触发 AJAX 请求的 error()
调用和 403 特定条件。
如果我在 403 特定错误调用中使用 alert()
,UI 将保持阻塞状态(根据需要)。
我想要做的 - 而不是使用 alert()
对话框 - 是使用 BlockUI 的模式对话框,它看起来好多了。
但是,一旦调用错误代码,错误模式对话框就会弹出并几乎立即消失,因为 AJAX 调用停止且 UI 解除阻塞。
如何保持 UI 被阻止以便错误模式保持显示?
这是相关代码:
$(document).ready(function() {
...
$('#modalErrorOK').click(function() {
$.unblockUI();
return false;
});
});
function startValidateFormParameters() {
$.blockUI({
message: '<h1><span style="display:inline-block; vertical-align:middle"><img src="https://example.com/resources/indicator.gif"/></span>\
Validating form parameters...</h1>'
});
validateFormParameters();
}
function validateFormParameters() {
$.ajax({
url: "/validateFormParameters.pl",
type: "POST",
data: ({"formSummary" : JSON.stringify($.foobar.formSummary)}),
dataType: "JSON",
cache: false,
success: function (response){
alert(JSON.stringify(response));
},
error: function (request, status, error) {
if (request.status == 401) {
window.location.replace(document.location.href);
}
else if (request.status == 403) {
$('#modalErrorMsg').replaceWith('<div id="modalErrorMsg">' + request.responseText + '</div>');
$.blockUI({
message: $('#modalError')
});
}
else
alert(request.status + "\n" + request.responseText);
}
});
}
$(document).ajaxStop($.unblockUI);
我有一个 DOM 对象来保存错误对话框:
<div id="modalError" style="display:none; cursor: default">
<h1>Error</h1>
<div id="modalErrorMsg"></div>
<input type="button" id="modalErrorOK" value="OK" />
</div>
When I click a form's submit button, this calls the startValidateFormParameters()
function.
In this function, I block the UI with BlockUI and run an AJAX request that validates some information in the form.
If the form data is bad in a certain way, the AJAX url
returns a 403 error response, which fires the AJAX request's error()
call and the 403-specific condition.
If I use alert()
within the 403-specific error call, the UI remains blocked (as desired).
What I would like to do — instead of using an alert()
dialog — is use BlockUI's modal dialog, which looks a lot nicer.
However, once the error code is called, the error modal dialog pops up and disappears almost immediately, as the AJAX call stops and the UI is unblocked.
How do I keep the UI blocked so that the error modal remains displayed?
Here is the relevant code:
$(document).ready(function() {
...
$('#modalErrorOK').click(function() {
$.unblockUI();
return false;
});
});
function startValidateFormParameters() {
$.blockUI({
message: '<h1><span style="display:inline-block; vertical-align:middle"><img src="https://example.com/resources/indicator.gif"/></span>\
Validating form parameters...</h1>'
});
validateFormParameters();
}
function validateFormParameters() {
$.ajax({
url: "/validateFormParameters.pl",
type: "POST",
data: ({"formSummary" : JSON.stringify($.foobar.formSummary)}),
dataType: "JSON",
cache: false,
success: function (response){
alert(JSON.stringify(response));
},
error: function (request, status, error) {
if (request.status == 401) {
window.location.replace(document.location.href);
}
else if (request.status == 403) {
$('#modalErrorMsg').replaceWith('<div id="modalErrorMsg">' + request.responseText + '</div>');
$.blockUI({
message: $('#modalError')
});
}
else
alert(request.status + "\n" + request.responseText);
}
});
}
$(document).ajaxStop($.unblockUI);
I have a DOM object to hold the error dialog:
<div id="modalError" style="display:none; cursor: default">
<h1>Error</h1>
<div id="modalErrorMsg"></div>
<input type="button" id="modalErrorOK" value="OK" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,删除这一行:
当 ajax 完成时,将停止 UI 被解锁,您需要将其替换为:
然后在错误回调中像平常一样调用模式对话框。
然后在对话框的“确定”按钮上添加一个
.click()
侦听器:您可能还需要调用:
对于未显示对话框的错误回调的其他部分。
I would think, removing the line:
Will stop the UI being unblocked when the ajax finishes, you will need to replace it with:
Then call the modal dialog as normal in the error callback as you are doing.
Then add a
.click()
listener the OK button of the dialog:You may also need to call:
For the other parts of the error callback where you are not showing the dialog.