【发布时间】:2019-01-09 06:08:43
【问题描述】:
我有招聘网站。用户填写表格并从 PC 中选择 CV(简历)文件,点击发送按钮。这将调用一个 ajaxSubmit() 函数,该函数执行本地验证(从下面的代码中删除),如果确定,则执行 AJAX 文件上传。服务器端进行进一步验证并以 JSON 格式返回 SUCCESS 或 ERROR(加上错误文本)。
当用户点击发送按钮时,我打开一个模式#apply_working_modal,它只显示一个微调器图标。在 ajax.done 上,我关闭#apply_working_modal,如果有错误,将它们放在modal 的#apply_errors_modal 的主体上并打开它。
我第一次尝试时,故意出错,它按描述工作。如果我再试一次,但错误仍然存在,#apply_working_modal 会显示并且在我手动单击 X 或远离它之前不会关闭。
这似乎是一些时间问题,因为当您在出错后重新提交时,CV 文件已经在服务器上,因此 JSON 很快就会返回。事实上,如果我稍微限制一下网络,我就可以让它工作。
我尝试过等待模式事件的变体,还尝试了 setTimeout() 来延迟打开#apply_errors_modal
function ajaxSubmit(whichForm){
$("#apply_working_modal").modal("show");
console.log('showing apply_working_modal');
var applyRequest =$.ajax({
url: postUrl,
etc: etc
});
applyRequest.done(function( data, textStatus, jqXHR ) {
console.log(data);
$("#apply_working_modal").modal("hide");
console.log("hiding #apply_working_modal ");
if (data.STATUS =='SUCCESS') {
// validation OK at server
}
else {// validation error at server
$('#apply_working_modal').on('hidden.bs.modal', function () {
$("#apply_errors_modal_body").html(data.ErrorText);
$("#apply_errors_modal").modal("show");
console.log("on hidden of #apply_working_modal");
})
}
});
}
这是好/坏测试的控制台输出:
//Expected behaviour
15:58:45.981 myscript.js:179 showing apply_working_modal
// 10 seconds pass while server uploads file and returns errors in JSON
15:58:56.696 myscript.js:206 {STATUS: "ERROR", ErrorText: "* The Comments are too short."}
15:58:56.700 myscript.js:217 hiding #apply_working_modal
15:58:56.998 myscript.js:236 on hidden of #apply_working_modal
// Problem behaviour
15:59:26.704 myscript.js:179 showing apply_working_modal
15:59:26.723 myscript.js:206 {STATUS: "ERROR", ErrorText: "* The Comments are too short."}
15:59:26.724 myscript.js:217 hiding #apply_working_modal
// #apply_working_modal never gets hidden, have to manually close it
// manual dismiss of modal after waitinf 13 secs ....
15:59:38.525 myscript.js:236 on hidden of #apply_working_modal
15:59:38.526 myscript.js:236 on hidden of #apply_working_modal
// error modal shown but logged many times?
HTML:
<!-- apply working (spinner while sending cv) -->
<div id="apply_working_modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><i class="fa fa-hourglass-end"></i><span class="sr-only">Loading...</span> Sending your CV</h4>
<button type="button" class="close" data-dismiss="modal"><i class="fa fa-times-circle-o" aria-hidden="true"></i></button>
</div>
<div class="modal-body text-center">
<i class="fa fa-spinner fa-spin fa-5x fa-fw"></i><span class="sr-only">Loading...</span>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<!-- apply errors modal-->
<div id="apply_errors_modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 style='color:#cc0000;' class="modal-title"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> There were errors!</h4>
<button type="button" class="close" data-dismiss="modal"><i class="fa fa-times-circle-o" aria-hidden="true"></i></button>
</div>
<div id="apply_errors_modal_body" class="modal-body">
</div>
<div class="modal-footer">
<button data-dismiss="modal" type="button" style="float:left;" class="btn btn-outline-info">Cancel</button>
</div>
</div>
</div>
</div>
根据下面的答案/cmets 进行更新。
为了在发布问题时清楚起见,我简化了 ajax 请求,因为我认为这不是原因 - 在我的代码中,我使用 'etc:etc' 作为 ajax 参数,这是为了表明我简化了代码.我确实在使用缓存:假。完整调用如下(postUrl 在代码前面设置):
var applyRequest =$.ajax({
url: postUrl,
data: postData,
type: "POST",
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandler, false); // For handling the progress of the upload
}
return myXhr;
},
enctype: 'multipart/form-data',
cache: false,
processData: false, // tell jQuery not to process the data (reqd for use with FormData)
contentType: false, // tell jQuery not to set contentType
dataType: "json"
});
【问题讨论】:
-
只是一个想法,但我遇到了类似的问题。就我而言,禁用
ajax缓存有效。你试过在你的ajax请求中设置cache: false;吗? -
ajaxSubmit怎么叫? -
添加了显示我正在使用缓存的代码:false;。并且,通过单击提交按钮调用 ajaxSubmit
标签: jquery modal-dialog bootstrap-4