javascript - Ajax-发送前

标签 javascript jquery ajax

为了防止出现两次错误,我使用 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/

相关文章:

php - ajax post 然后获取要使用的文件

javascript - 将鼠标悬停在一个类(class)上会影响页面上同一类(class)的所有其他类(class)

php - 与 Elasticsearch 进行 AJAX 通信的流量和访问控制解决方案?

javascript - "red"值不是数字时文本框高亮显示,带点或逗号的数字

javascript - 通过 slickgrid 中的 seacrh 字段将滚动条移动到特定行

javascript - 跨平台移动浏览器中可滚动 div 中的滚动条

javascript - C3js 图表线在 grunt 构建后隐藏

javascript - 使用 css 将页面更改为编辑模式

javascript - 如何在 Ajax 响应后刷新特定的 div?

ajax - 如何取消/中止 jQuery AJAX 请求?