weixin_33739523 2015-07-16 15:19 采纳率: 0%
浏览 45

求助: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才会发生。由于某些原因,如果用户在Submit按钮上单击多次(非常快),Ajax也会发生。

  • 写回答

3条回答 默认 最新

  • 胖鸭 2015-07-16 15:23
    关注

    To prevent this kind of issue disable the button before sending the ajax and then anable inside the success function

    $(mybutton).prop("disabled",true);
    // ajax call here
    

    then

    success: function(data, textStatus, jqXHR) {
        $(mybutton).prop("disabled",false);
        // code here
    }
    
    评论

报告相同问题?