简体   繁体   English

成功将记录添加到数据库 (ajax) 时,Javascript 会自动发出警报

[英]Javascript automatically alerts when successfully adding a record to database (ajax)

I'm building out a simple chat app and every time I hit the send button (which adds a record to the database) I get an alert saying "Success".我正在构建一个简单的聊天应用程序,每次我点击发送按钮(将记录添加到数据库)时,我都会收到一条提示“成功”。 Nowhere in my code do I tell it to give me a success alert, only an error alert.在我的代码中没有任何地方告诉它给我一个成功警报,只有一个错误警报。 Here is the code that gets called when the send button is clicked这是单击发送按钮时调用的代码

function formCheck(e){
e.preventDefault();
// ensure message has text
if(msg.value.trim() != ""){
    $.ajax({
        type: 'POST',
        url: '../handle_message.php',
        data: {
            name: name,
            color: color,
            msg: $( "#msg" ).val(),
        },
        datatype: 'JSON',
        success: function(bool){
            if (bool == "failure") {
                alert("Failure Inside Success function");
            }
        },
        error: function(){
            alert("Failure, could not send message");
        },
    });
}
}

The php: php:

require_once("Database.php");
$dbc = $conn->getConnection();

$sql = "INSERT INTO Table (name, color, msg) VALUES (:name, :color, :msg)";
$statement = $dbc->prepare($sql);
$statement->bindValue(":name", $_POST['name']);
$statement->bindValue(":color", $_POST['color']);
$statement->bindValue(":msg", $_POST['msg']);
$result = $statement->execute();

if(!$result){
    echo "failure";
}

The php works how it's supposed to. php 以它应该的方式工作。 I'm just not sure why I'm getting the success popup after clicking the button.我只是不确定为什么单击按钮后会弹出成功。 Any help would be appreciated.任何帮助,将不胜感激。

Update: I was only testing this in Google Chrome.更新:我只是在谷歌浏览器中测试这个。 Once I switched over to Firefox the alert went away, I guess it was something with chrome?一旦我切换到 Firefox,警报就消失了,我猜是 chrome 的问题?

Going back to the Google Chrome tab, holding the shift key and pressing refresh did the trick!回到谷歌浏览器选项卡,按住 shift 键并按刷新就可以了! thanks to all that took the time to comment感谢所有花时间发表评论的人

Shift Reload换档重装

Holding the shift key down when requesting a refresh tells the browser to reload everything from the server without using cached copies.请求刷新时按住 shift 键会告诉浏览器从服务器重新加载所有内容,而不使用缓存副本。 When viewing pages which use the file:// protocol it should ensure page files are reloaded from local storage rather than (say) history snapshots, but if problems persist close and re-open the browser.查看使用file://协议的页面时,应确保从本地存储重新加载页面文件而不是(例如)历史快照,但如果问题仍然存在,请关闭并重新打开浏览器。

I probably learned of this in Netscape documentation last century - where it's documented now would require time and a more intensive search than the one I tried.我可能在上个世纪的 Netscape 文档中了解到这一点 - 现在记录它需要时间和比我尝试过的更密集的搜索。


FWIW Technical details about headers sent to the server are covered in this answer on Super User. FWIW 有关发送到服务器的标头的技术详细信息包含在超级用户的此答案中

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

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