简体   繁体   English

ajax/jquery POST 请求的不必要重复

[英]Unwanted repeats of ajax/jquery POST request

I'm running into an issue where my post requests are being repeated for reasons I cannot comprehend.我遇到了一个问题,由于我无法理解的原因,我的帖子请求被重复。 I'm using a jQuery POST request to submit a form to a php script to insert it into an SQL database.我正在使用 jQuery POST 请求将表单提交到 php 脚本,以将其插入 SQL 数据库。

It only happens on some occasions and not consistently.它只在某些情况下发生,而且并非始终如一。 9/10 times it works as intended, but sometimes the ajax call is repeated 2 or 3 times. 9/10 次按预期工作,但有时 ajax 调用重复 2 或 3 次。

enter image description here在此处输入图像描述

repeating block of code:重复代码块:

function submitLog(){ function submitLog(){

let log = document.getElementById('logContent').value;
let project = document.getElementById('logger_active_project').innerHTML;
let category = document.getElementById('categorySelect').value;
let projectID = document.getElementById('logger_active_project_id').value;
let submit = document.getElementById('submit');
submit.disabled = true;

console.log('starting ajax post request');

$.post('./includes/logger/scripts/add_log.php', {
    log:log,
    project:project,
    category:category,
    project_id:projectID

}, function(data, status){
    document.getElementById('logContent').value= "";
    submit.disabled = false;
    console.log('ajax callback fired.' + data);
    
})

} EDIT: } 编辑:

It seems to only be happening when used with the following function:它似乎只在与以下 function 一起使用时才会发生:

function submitLogByEntering(){ let log = document.getElementById('logContent'); function submitLogByEntering(){ let log = document.getElementById('logContent');

log.addEventListener("keyup", function(event) {
    // Number 13 is the "Enter" key on the keyboard
    if (event.keyCode === 13) {
      // Cancel the default action, if needed
      event.preventDefault();
      submitLog();
    }
  });
}

When using the button to duplicate the issue i can not get it to repeat.当使用按钮复制问题时,我无法让它重复。

Thanks a bunch in advance for any tips to get me in the right direction to fix this hot mess!提前非常感谢您提供的任何提示,让我找到正确的方向来解决这个热点问题!

It will most likely be down to the listener for submitLog() .它很可能submitLog()的侦听器。

Check that you're not using a listener that would multi fire.检查您没有使用会多重触发的侦听器。

If it's to much to check just set a test variable outside of your submitLog() , set it to true and test if true then fire the ajax.如果检查太多,只需在submitLog()之外设置一个测试变量,请将其设置为 true 并测试是否为 true,然后触发 ajax。 Then set it to false after your post request.然后在您的发布请求后将其设置为 false 。 This will stop duplicate button presses etc from happening or anything funny in the listener.这将阻止重复的按钮按下等发生或听众中任何有趣的事情。

If you need to re fire the function set a timeout on the variable (Or set it after ajax completion) to reset it back to true.如果您需要重新触发 function 对变量设置超时(或在 ajax 完成后设置)以将其重置为真。

EXAMPLE:例子:

 var test = true; function submitLog() { if (test) { let log = document.getElementById('logContent').value; let project = document.getElementById('logger_active_project').innerHTML; let category = document.getElementById('categorySelect').value; let projectID = document.getElementById('logger_active_project_id').value; let submit = document.getElementById('submit'); submit.disabled = true; console.log('starting ajax post request'); $.post('./includes/logger/scripts/add_log.php', { log: log, project: project, category: category, project_id: projectID }, function (data, status) { document.getElementById('logContent').value = ""; submit.disabled = false; console.log('ajax callback fired.' + data); }); test = false; setTimeout(function () { test = true; }, 5000); } }

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

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