如果用户填写了一些无效数据,则会触发 ajax 请求并显示错误消息。现在,当用户再次更正数据/或再次输入无效数据时,会触发 2 个请求,下一次是 3 个,并且会一直累加。
这可能是因为 parsley js 库。如果我删除欧芹代码,它就可以正常工作。有什么想法吗?
这是ajax代码
$('#upload-profile-button').on('click', function(e) {
$("#upload-profile-form").parsley().validate();
$("#upload-profile-form").parsley().on('form:validate', function (formInstance) {
var isFormValid = formInstance.isValid();
if(isFormValid){
e.preventDefault();
$('#upload-profile-button').html('Uploading...');
var fd = new FormData($("#upload-profile-form")[0]);
$.ajax({
type : 'POST',
url : '/mbatch/batch/uploadBatch',
data : fd,
processData : false,
contentType : false,
beforeSend : function() {
},
success : function(response) {
if (response.data.fetchTpodSuccess == true) {
window.location.href= "/mbatch/batch/listBatch";
} else {
new PNotify({
title: 'Notice',
text: response.message,
type: 'error',
styling: 'bootstrap3'
});
$('#upload-profile-button').html('Submit');
}
},
error : function(data) {
new PNotify({
title: 'Notice',
text: JSON.parse(data.responseText).message,
type: 'error',
styling: 'bootstrap3'
});
$('#upload-profile-button').html('Submit');
}
});
}
});
});
这是 HTML 代码片段
<button id="upload-profile-button" type="button"
class="btn btn-primary">Submit</button>
任何线索将不胜感激。
最佳答案
我刚刚找到了解决方案。 这是因为我也在表单标签中使用了 data-parsley-validate 在 js 中也是如此
<form id="upload-profile-form" data-parsley-validate enctype="multipart/form-data"
name = "uploadBatchForm"
class="form-horizontal form-label-left"
>
Parsley 在文档加载时查看 DOM 中所有出现的 data-parsley-validate 并在有效时自动绑定(bind)它们。 一旦表单或字段实例被 Parsley 绑定(bind),执行 $('#form').parsley(options);将更新现有选项但不会替换它们。
关于javascript - Ajax 请求附加到多个命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211356/