简体   繁体   English

ajax GET请求responseText为空

[英]ajax GET request responseText empty

I'm trying to create a chatbox using AJAX but for some reason my xhttp.responseText is empty. 我正在尝试使用AJAX创建聊天框,但是由于某种原因,我的xhttp.responseText为空。 In firebug I can see that a GET request is being sent and it even responds with the correct text, but this text just doesn't get put in the responseText for some reason. 在萤火虫中,我可以看到正在发送GET请求,它甚至使用正确的文本进行响应,但是由于某种原因,该文本只是没有被放入responseText中。

Here is my index.html: 这是我的index.html:

<!doctype html>

<html>

<head>
    <meta charset="utf-8"/>
    <title>Chatroom</title>
    <script>

    function setup() {
        ajaxRequest( 'GET', 'loadmessages.php', updateChat);
        setInterval(function () {
            ajaxRequest( 'GET', 'loadmessages.php', updateChat);
        }, 1000);
    }

    function updateChat(xhttp) {
        document.getElementById( 'chat' ).innerHTML = xhttp.responseText;
    }

    function ajaxRequest( method, file, cfunc ) {
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if(xhttp.readyState == 2 && xhttp.status == 200) {
                cfunc(xhttp);
            }
        }
        xhttp.open( method, file, true);
        xhttp.send();
    }

    </script>
</head>

<body onload="setup();">
    <div id="chat">

    </div>
</body>

</html>

Here is loadmessages.php: 这是loadmessages.php:

<?php 

include( 'connect.php' );


$query = "SELECT * FROM messages ORDER BY id DESC";
$result = mysqli_query($conn, $query);

if( mysqli_num_rows($result) > 0 ) {
    $output = "";
    while( $row = mysqli_fetch_assoc($result) ) {
        $id = $row['id'];
        $name = $row['name'];
        $content = $row['content'];
        $time = $row['time'];

        $output .= "[sent by $name on $time] $content <hr/>";
    }
    echo $output;
} else {
    echo "No messages yet, be the first to send one!";
}

mysqli_close($conn);
?>

And the connect.php: 和connect.php:

<?php 

$conn = mysqli_connect( 'localhost', 'root', '', 'chatroom' ) or die( 'Couldn\'t connect to database!' );

?>

Since there's nothing in the database yet, it just echoes "No messages yet, be the first to send one!". 由于数据库中还没有任何内容,它只会显示“尚无消息,请第一个发送消息!”。 I can see this response if I open firebug, but this text is not in the responseText variable. 如果打开Firebug,我可以看到此响应,但是此文本不在responseText变量中。

You should change the if clause for readyState like below: 您应该更改readyStateif子句,如下所示:

xhttp.onreadystatechange = function () {
    if(xhttp.readyState == 4) {
        cfunc(xhttp);
    }
}

since this callback is triggered everytime the readyState changes and you are testing for the value 2 which is sent , at this point there is no response available in xhttp.responseText 由于每次readyState更改时都会触发此回调,并且您正在测试sent的值2 ,因此xhttp.responseText没有可用的xhttp.responseText

See here What do the different readystates in XMLHttpRequest mean, and how can I use them? 请参阅此处XMLHttpRequest中不同的readystate是什么意思,我该如何使用它们?

In slightly more detail here Why XmlHttpRequest readyState = 2 on 200 HTTP response code 在这里稍微详细一点, 为什么XmlHttpRequest readyState = 2 on 200 HTTP response code
the difference between readyState==2 and readyState==4 readyState==2readyState==4之间的区别

I highly recommend using jQuery for AJAX, because it is far more simple and intuitive. 我强烈建议将jQuery用于AJAX,因为它更加简单直观。 Here is link for more info: http://www.w3schools.com/jquery/jquery_ref_ajax.asp 这是更多信息的链接: http : //www.w3schools.com/jquery/jquery_ref_ajax.asp

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

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