duanaixuan7385 2017-12-20 07:55
浏览 66
已采纳

JavaScript HttpRequest始终返回相同的结果

I have a HTML page JavaScript which send a GET request data to PHP file to return all datas saved in the database . PHP replies with a HTML-table - that works fine!

But: When if i click a button (which calls the same JavaScript function) to update my table in order to display the new data, i get the same result (and i have definitely new data on table).

If I call the PHP manually via the browser it'll show me the new results immediately and at this moment it is also working with JavaScript (but only once).

Here is a part of my code.

HTML/JS:

<button onclick="GetData()"></button>
<div id="test"></div>
<script>

    function GetData(){
        var xhttp = new XMLHttpRequest();
        document.getElementById("test").innerHTML = "";
        xhttp.onreadystatechange = function(){
            if (xhttp.readyState == 4 && xhttp.status == 200){
                document.getElementById("test").innerHTML = xhttp.responseText;

            }
        };

        xhttp.open("GET", "../GetData.php", true);

        xhttp.send();
        }

</script>

PHP:

    //DB details
    $dbHost     = 'localhost';
    $dbUsername = 'lalalala';
    $dbPassword = 'lalalalal';
    $dbName     = 'lalalala';

    //Create connection and select DB
    $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName) or die ("UUUUPS");


    $sql = "select name, beschreibung, image, video from data";



    $result = $db->query($sql);


   if ($result->num_rows > 0) {
$return = '<table class ="table table-hover"><thead><tr><th scope="col">Name</th><th scope="col">Beschreibung</th><th scope="col">Bilddatei</th><th scope="col">Video-Url</th></tr></thead><tbody>';
// output data of each row
while($row = $result->fetch_assoc()) {
    $return .= "<tr><td>".$row["name"]."</td><td>".$row["beschreibung"]."</td><td><img src=data:image/png;base64,".base64_encode($row["image"])."/></td><td>".$row["video"]."</tr>";
}
$return .= "</tbody></table>";
 $db->close();
 echo $return;
 } else {
 echo "0 results";
 }

Thank you for your help!

  • 写回答

2条回答 默认 最新

  • donglv5269 2017-12-20 08:04
    关注

    It seems your browser is caching your result, that's why you see data. You can test it like this:

    var random = Math.floor(Math.random() * 100);
    xhttp.open("GET", "../GetData.php?"+random, true);
    

    If this helps, look into expire headers in your PHP script. Also, the way you're doing queries in quite outdated. It's a very PHP4 way. Have a look here: http://php.net/manual/en/book.mysqli.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题