AJAX调用多次触发 [英] AJAX call fired multiple times

查看:62
本文介绍了AJAX调用多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php应用程序将excel文件上传到服务器.我使用ajax将数据发送到服务器.但是有时ajax调用可以重复工作.我在按钮单击中调用了ajax函数,

I have a php application to upload excel files to server.I use ajax to send data to server.But some times the ajax call works repeatedly.I called the ajax function in button click as

$(document).on("click", "#btnContinue", function() {
    $.ajax({
        url: "ExcelColDesptn.php",
        data: data,                           
        type: 'post',
        success: function(response) {}
    });

HTML:

<button id="btnContinue" name="btnContinue" class="btn btn-primary ">Continue </button> 

我使用Firebug对其进行修复,它显示

I use firebug to fix it, it shows

我不知道为什么会这样.在这里,"ExcelColDesptn.php"被调用3次,而"SaveExcelToServer.php"被调用4次.有时,它恰好工作1次.有帮助吗?

I don't know why it happens. Here 'ExcelColDesptn.php' is called 3 times and 'SaveExcelToServer.php' is called 4 times. Sometimes it works exactly 1 time. Any help?

推荐答案

您拥有的函数,该函数绑定在另一个函数中吗?多次被称为?因为这是一个LIVE绑定,所以即使内容是更新的,该绑定也存在.

The function you have, is this binding in another function? Which is called multiple times? Because this is a LIVE binding which means, the binding exists even if the content is update.

所以我期望以下几点:

  • 您使用ajax更新内容
  • #btnContinue是否在由Ajax更新的HTML元素内?
  • 您调用该函数向按钮添加新绑定
  • 该按钮获得了额外的点击绑定
  • 因此更新ajax的次数越多,执行上传的次数就越多.由于您绑定点击的方式.

解决方案:

  1. 将其设为普通绑定 $(#btnContinue").on("click",fn)
  2. 仅在准备就绪的文档中放置绑定,而不在多次调用的函数中放置
  3. 在新绑定之前使用 $(#btnContinue").off("click")(这是最丑的解决方案)
  1. Make it a normal binding $("#btnContinue").on("click", fn)
  2. Put your binding only in document ready, and not in a function which is called multiple times
  3. use $("#btnContinue").off("click") before your new binding (which is the most ugly solution)

这篇关于AJAX调用多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆