Richi Sharma
Richi Sharma

Reputation: 185

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.

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>
    } 

Upvotes: 0

Views: 2469

Answers (1)

Richi Sharma
Richi Sharma

Reputation: 185

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.

Upvotes: 2

Related Questions