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

带有Ajax的JavaScript和HTTP请求输出未定义的显示2,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在制作一个本地聊天应用程序,我的问题是,它显示未定义的结果,然后在显示所有数据后工作正常 代码如下: function chatDisplay(){ if(mainchat.msg.value== ""){ alert("fill in blanks"); return; } var uname = $_SESSION['username']; var msg = mainchat

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

代码如下:

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;
可以不定义reponseText的一种方法是,如果返回是(或看起来像)XML,则改为设置responseXML

快速“修复”是:

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

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

尝试类似的方法。请让我知道它对你有没有帮助

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

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

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

先生,它仍然显示未定义!那么,我猜它来自于你没有和我们分享的东西。先生,它仍然显示未定义!然后,我猜它来自于一些你没有和我们分享的东西。这对测试很有帮助。sir@yatindra使用您的这段代码,这篇文章现在显示0而不是previos UndefinedTerry this->if(typeof xhttp.reponseText!='undefined'){document.getElementById(“chatlogs”).innerHTML=xhttp.reponseText;}相同的输出未定义nowvar responseMsg=xhtp.reponseText |;如果(responseMsg!=''&&responseMsg!=0){document.getElementById(“chatlogs”).innerHTML=responseMsg;}insert.php中的代码返回了什么?这对测试很有帮助。sir@yatindra使用您的这段代码,这篇文章现在显示0而不是previos UndefinedTerry this->if(typeof xhttp.reponseText!='undefined'){document.getElementById(“chatlogs”).innerHTML=xhttp.reponseText;}相同的输出未定义nowvar responseMsg=xhtp.reponseText |;如果(responseMsg!=''&&responseMsg!=0){document.getElementById(“chatlogs”).innerHTML=responseMsg;}insert.php中的代码是什么它返回什么?