为了防止出现两次错误,我使用 beforeSend。
hasSent = false
function submit() {
if (!hasSent)
$.ajax({
url: "${createLink(controller:'userInvitation', action:'ajaxUpdate')}",
type: "POST",
data: $("#invitationForm").serialize(),
success: function(data, textStatus, jqXHR) {
$('#invitationForm')[0].reset();
$('.thank-you-modal').modal('show');
hasSent = true;
console.log(hasSent)
},
complete: function() {
hasSent = false;
console.log(hasSent)
}
});
}
如您所见,只有当 hasSent=false
时,ajax 才会发生。
由于某种原因,如果用户多次(非常快地)单击提交按钮,ajax
也会发生
最佳答案
为了防止此类问题,请在发送 ajax 之前禁用按钮,然后在成功函数内启用
$(mybutton).prop("disabled",true);
// ajax call here
然后
success: function(data, textStatus, jqXHR) {
$(mybutton).prop("disabled",false);
// code here
}
关于javascript - Ajax-发送前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31458277/