SQL插入已完成,但AJAX检索错误消息



我有以下 javascript 代码:

var value = $.ajax({
        type: "POST",
        url: "receive.php",
        data: $('#something').serialize(),
        cache: false,
        async: true
    }).success(function(){
    }).error(function() {
        alert("not sent");
    });

php

if(isset($_POST['the_thingy'])){
    $allowed = 100;
    $the_thingy = $mysqli->real_escape_string($_POST['the_thingy']);
    $query = "SELECT another thingy from the database";
    $the_thingy_length = strlen($the_thingy);
    if($the_thingy_length <= $allowed){

        $query = "INSERT INTO thingyes VALUES '$the_thingy'";
        $result = $mysqli->query($query);

    }
//amongst other queries and of the same "type"

基本上发生的是插入确实是完成的,但由于某种原因,它在数据库中看起来会加倍(2倍查询一次,一次进行了一次)。Ajax检索 alert("未发送"); 和页面刷新(不假定)。可能是什么原因造成的?其他查询,代码语法,某些函数?我使用的所有其他Ajax都很好。这是由于特定的原因而成为已知问题吗?非常感谢您!

对于双重提交和页面刷新,听起来您需要防止表单的默认操作:

$("form").on('submit', function(event) {
  // prevent the default submit
  event.preventDefault();
  // do your other stuff
  // your ajax call
  var value = $.ajax({
       ...
    });
});

如果条目得到正确添加,但是您可以在错误函数上添加相同的错误功能,则有可能在PHP中有一个错误,这会导致脚本带有错误退出(插入部分之后的某个地方)。

您应该启用错误处理和显示以在浏览器工具的"网络"选项卡中查看(或临时禁用Ajax以正态提交并在浏览器中查看错误)或检查消息中的Web-Server错误日志中的Web-Server错误日志。/p>

最新更新