本文介绍了防止ajax调用两次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ajax电话
$('#button1').on('click', function(e){
$.ajax({
url: url,
type: 'POST',
async: true,
dataType: 'json',
enctype: 'multipart/form-data',
cache: false,
success: function(data){
},
error: function(){}
});
e.stopImmediatePropagation();
return false;
});
现在,我们会在10分钟后收到回复。所以ajax调用被多次调用。为什么会发生这种情况/我们如何确保只调用一次ajax调用?
Now here the response is received after 10 minutes . So the ajax call is called multiple times. Why does this happen / how can we ensure that ajax call is called only once?
推荐答案
禁用按钮的替代方法是使用方法并在回调后重新绑定事件处理程序:
An alternative to disabling the button would be to use the .one() method and re-bind the event handler after callback:
var clickHandler = function(e){
$.ajax({
url: url,
type: 'POST',
async: true,
dataType: 'json',
enctype: 'multipart/form-data',
cache: false,
success: function(data){
$('#button1').one('click', clickHandler);
},
error: function(){}
});
e.stopImmediatePropagation();
return false;
}
$('#button1').one('click', clickHandler);
这篇关于防止ajax调用两次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!