简体   繁体   English

AJAX请求被多次发送

[英]AJAX request is sent multiple times

There are many other post related to the issue, but I couldn't find one that can solved my issue. 还有很多与此问题相关的帖子,但是我找不到可以解决我的问题的帖子。 The AJAX request is sent multiple times. AJAX请求被多次发送。

var checkAllStatus = function () {
    $.ajax({
        cache: false,
        type: "POST",
        url: "@Url.Action("CheckAllStatus")",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            if (data == true) {
                DoCloseDay();
            }
            else {
                window.location.href(root + 'Close');
            }
        },    
        error: function (xhr, ajaxOptions, thrownError) {     
            alert('Error: ' + thrownError);    
        }    
    });
};

function DoCloseDay() {    
    var statesProgress = $("#loading-progress");    
    var successMsg = $("#success-msg");    
    statesProgress.show();    
    $.ajax({    
        cache: false,    
        type: "POST",    
        url: "@Url.Action("Close")",    
        dataType: 'json',    
        contentType: "application/json; charset=utf-8",    
        success: function (data) {    
            if (data == true) {    
                window.location.href(root + 'Close');    
            }    
        },    
        error: function (xhr, ajaxOptions, thrownError) {    
            alert('Error: ' + thrownError);    
        }            
    });            
} 

the controls is enabled and disabled based on the condition as mentioned below. 控件将根据以下条件启用和禁用。 i don't think that will make the request to be send twice 我不认为这会使请求发送两次

@if (Model.SafeDropsTask.Status == 'SAVED' )
    {
        <button type="button" id="close" name="close" onclick="checkAllStatus();">
            <i class="fa fa-check-circle"></i>&nbsp;Close
        </button>
    }
    else
    {
        <button type="button" id="close" name="close" onclick="checkAllStatus();" disabled="disabled">
            <i class="fa fa-check-circle"></i>&nbsp;Close
        </button>
    } 

I have found the reason of the issue i was facing. 我已经找到了我所面临的问题的原因。 Because of slow browser, user was able to click the button multiple times. 由于浏览器运行缓慢,因此用户能够多次单击该按钮。 to prevent this i have made the button disabled as soon as the user click for the first time. 为了防止这种情况,我在用户第一次单击后立即禁用了该按钮。 Thanks a lot to the people who have provided their valuable comments. 非常感谢提供宝贵意见的人们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM