还有很多与该问题相关的帖子,但我找不到可以解决我的问题的帖子。 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);
}
});
}
控件根据下面提到的条件启用和禁用。我认为这不会使请求发送两次
@if (Model.SafeDropsTask.Status == 'SAVED' )
{
<button type="button" id="close" name="close" onclick="checkAllStatus();">
<i class="fa fa-check-circle"></i> Close
</button>
}
else
{
<button type="button" id="close" name="close" onclick="checkAllStatus();" disabled="disabled">
<i class="fa fa-check-circle"></i> Close
</button>
}
最佳答案
我已经找到了我所面临问题的原因。由于浏览器速度较慢,用户可以多次单击该按钮。为了防止这种情况,我在用户第一次单击时立即禁用该按钮。非常感谢那些提出宝贵意见的人。
关于javascript - AJAX请求发送多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596819/