老实说,我对这个编程世界非常陌生,我正在努力学习我当前的项目。我基本上是把这些放在StackOverflow的帖子上
目标:我希望从我的MySQL数据库中提取数据,并将这些数据显示给客户端,并让它在不需要刷新的情况下间隔更新。
我目前在本地主机上运行的代码,但是当我将它移植到我的web主机时,它就停止了正常工作。目前,它将提取数据,当它执行刷新更新时,它会从旧的PHP文件中提取数据,除非您手动将PHP文件重新加载为客户端。
ctemp.php
include_once 'db_connect.php';
$sql = "SELECT * FROM weather WHERE id=1;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)){
$ctemp = $row['temp'];
print($ctemp);
}
}data.php
<?php>
include_once 'api/db_connect.php';
include 'header.php';
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<body class="body1">
<div class="container-fluid">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
<h1 class="display-10 fw-bold">Sensor Data</h1>
</header>
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<div>
<script type="text/javascript">
function doRefresh(){
$("#show").load("/api/ctemp.php");
}
setInterval(function(){doRefresh()}, 5000);
</script>
<h1 class="display-5">
The current temperature is: <b><span id="show"><?php include_once 'api/ctemp.php' ; ?></span></b><b><span>°</span>C</b> or <b><span id="show2"><?php include 'api/ftemp.php' ; ?></span></b><b><span>°</span>F</b>
</h1>
</div>
<br>
</body>发布于 2022-10-07 04:29:54
将$.ajaxSetup({cache: false});添加到您的javascript代码的顶部(在function doRefresh(){的上方应该是一个很好的地方)。
https://stackoverflow.com/questions/73981985
复制相似问题