duanhuayong6687 2019-07-30 16:18
浏览 381

似乎没有执行AJAX请求

Purpose: Live PHP chat system

About the code:

  1. The first part searches for older messages and displays them (it works). It also gets the ID of the last message that already exists.
  2. The second part searches for new messages (their ID is greater than the one received on page loading).

Problem:

  1. The AJAX request doen't seem to be executed since new text isn't displayed. Also, anything written after the div id="verification" doesn't work: for ex I tried displaying an alert, but without success.
  2. I get an error in the Chrome console: Uncaught syntax error: Invalid or unexpected token on this line (and it might be beacuse of the '<div>' in the echo statement:

    echo '<div>' . $row['com'] . '</div>';

Main code:

<?php
   include('config.php');
   $query = "SELECT MAX(id) FROM comments";
        $res = mysqli_query($connect,$query); 
        $rez = mysqli_fetch_assoc($res);
        $id = $rez['MAX(id)'];

        $query1 = "SELECT com FROM comments";
        $res1 = mysqli_query($connect,$query1); 
    ?>
    <!DOCTYPE html>
    <html>

    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <style>
    .verification{
      overflow-y:scroll;
      height:300px;
      width:100px;
    }
    </style>

    </head>

    <body>

    <div id="verification">
    <?php
    while($row = mysqli_fetch_assoc($res1))
             { 
                 echo '<div>' . $row['com'] . '</div>';
             }   
    ?>
    </div>

    <script>

    setInterval(function () {
    $.ajax({
       type: "GET",
       url: "getmsg.php?id=<?php echo $id; ?>",
       dataType: "json",
       success: function(data) {
         for (i = 0; i < data.length; ++i) {
           var msg = data[i][0];
           $("#verification").append('<div class="left spotmsg"><div class="message">
           <p class="txt">'+msg+'</p></div></div>');
           }
        }
      });
    }, 1000);

    </script>

    </body>

    </html>

getmsg.php:

<?php
include('config.php');
$id1 = $_GET['id'];
$query = "SELECT com FROM comments WHERE id > $id1";
$res = mysqli_query($connect,$query);

while ($row = mysqli_fetch_all($res)) {
header('Content-Type: application/json');
ob_end_clean();
echo json_encode($row);
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。