带有Ajax的JavaScript和HTTP请求输出未定义的显示2

JavaScript with Ajax with HTTP request outputs undefined display 2

本文关键字:输出 未定义 显示 请求 HTTP Ajax JavaScript 带有      更新时间:2023-09-26

我正在制作一个本地聊天应用程序,我的问题是它显示未定义的结果,然后在显示所有数据后工作正常。

这是代码:

function chatDisplay(){
        if(mainchat.msg.value== ""){
            alert("fill in blanks");
            return;
        }
        var uname = $_SESSION['username'];
        var msg = mainchat.msg.value;
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function(){
            if(xhttp.readyState == 4 && xhttp.status==200){
                document.getElementById("chatlogs").innerHTML = xhttp.reponseText;
            }
        };
        xhttp.open("GET", "insert.php?uname="+uname+"&msg="+msg, true);
        xhttp.send();
        $(document).ready(function(){
            $.ajaxSetup({cache:false});
            setInterval(function(){
                $("#chatlogs").load("logs.php");}, 2000);
        });
    }

聊天图像

我能看到的唯一可能发生这种情况的地方是这行:

document.getElementById("chatlogs").innerHTML = xhttp.reponseText;

可以定义reponseText的一种方法是,如果返回是(或看起来像)XML,那么在这种情况下会设置responseXML。

一个快速的"解决方案"是:

document.getElementById("chatlogs").innerHTML
    = xhttp.reponseText ? xhttp.reponseText : "";

当然,问题可能出在您没有与我们共享的代码中。

试试这样的方法。请让我知道它对你有没有帮助???

document.getElementById("chatlogs").innerHTML = xhttp.reponseText | "";