我有一个web应用程序,它使用ajax加载页面的主要内容。我使用jquery地址来处理ajax导航。我有一个表单显示在模态中,成功提交后,我想将用户重定向到一个新页面。这是代码:
//called when form is submitted
$('#modal-form').on('submit', 'form', function (e) {
e.preventDefault();
var url = $(this).attr('action');
var data = $(this).serialize();
var method = $(this).attr('method');
$.ajax({
url: url,
data: data,
dataType: 'html',
method: method
}).fail(function (xhr, status, err) {
// Check for 401 unauthorized
if (xhr.status === 401) window.location = '/auth/login/';
}).done(function (data, status, xhr) {
$('#modal-form').modal('hide'); //Hide the modal
redirect(xhr); //Redirect to a new page
});
});
现在,问题是在模态完全隐藏之前,页面重定向会导致它变黑且无法访问。我也尝试将重定向调用移动到完整的函数中,但似乎不起作用。如果你需要任何其他信息,请告诉我。
在模态完成后,您必须使用模态回调来运行重定向:
$('#modal-form').on('hidden.bs.modal', function (e) {
redirect(xhr);
});