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

时间:2022-11-05 14:25:49

I'm making a local chat application my problem is that it displays undefined result then works fine after display all data.

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

here's the code:

这是代码:

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);
        });
    }

chat image

聊天图片

2 个解决方案

#1


0  

The only place I can see where this could happen is this line:

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

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

One way that reponseText can be undefined is if the return is (or looks like) XML, in which case responseXML is set instead.

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

A quicky "fix" would be:

一个快速的“修复”将是:

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

Of course, it's possible the problem is in code you didn't share with us.

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

#2


-1  

Try something like this. And please let me know its helpful for you or not ???

尝试这样的事情。请让我知道它对你有帮助吗???

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

#1


0  

The only place I can see where this could happen is this line:

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

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

One way that reponseText can be undefined is if the return is (or looks like) XML, in which case responseXML is set instead.

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

A quicky "fix" would be:

一个快速的“修复”将是:

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

Of course, it's possible the problem is in code you didn't share with us.

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

#2


-1  

Try something like this. And please let me know its helpful for you or not ???

尝试这样的事情。请让我知道它对你有帮助吗???

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