繁体   English   中英

使用jquery刷新php并重新加载图像

[英]Refresh php with jquery and reload image

我有这个php页面/var/www/oggi1ora.php

首先,我需要执行页面/usr/local/bin/oggi1ora.php来生成图表。

每隔5秒PHP脚本正确执行,但页面中的图像不刷新...我该如何解决?

我需要每5秒执行一次/usr/local/bin/oggi1ora.php 该脚本生成存储在/var/www/grafici/oggi1ora.png图像。

<div class="result">
    <?php exec('php -q /usr/local/bin/oggi1ora.php'); ?>
</div>

<div class="oggi1ora">
<html>
    <body>
       <img src="grafici/oggi1ora.png" id="oggi1ora" border="0" />
    </body>
</html>
</div>

<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script>
    function refresh_div() {
        jQuery.ajax({
            url:'oggi1ora.php',
            type:'POST',
            success:function(results) {
                jQuery(".result").html(results);
            }
        });
    }
    t = setInterval(refresh_div,5000);
</script>

这是新的/var/www/oggi1ora.php

<div class="result">
<?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");
    include('/usr/local/bin/oggi1ora.php');
?>
</div>

<div class="oggi1ora">
<html>
    <img src="grafici/oggi1ora.png" id="oggi1ora" border="0" />
</html>
</div>

<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script>
    function refresh_div() {
        var d = new Date().getTime();
        jQuery.ajax({
        url:'oggi1ora.php?ts=' + d,
        type:'POST',
            success:function(results) {
            $("#oggi1ora").attr("src", "grafici/oggi1ora.png?ts=" + d);
            }
        });
    }
    t = setInterval(refresh_div,5000);
</script>

您可能有浏览器缓存问题。 一种解决方案是在URL中附加时间戳以确保获取最新版本。 或者将服务器端标头设置为不缓存。 对于以前的解决方案,您可以尝试这样做:

function refresh_div() {
    var d = new Date().getTime();
    jQuery.ajax({
        url:'oggi1ora.php?ts=' + d,
        type:'POST',
        // rest of your code

对于后者,您可以设置如下标题:

<?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");

接下来, jQuery(".result").html(results)可能没有做任何事情,因为你的标记中没有带有“result”类的元素。

相反,忽略结果并强制图像刷新,如下所示:

function refresh_div() {
    var d = new Date().getTime();
    jQuery.ajax({
        url:'oggi1ora.php?ts=' + d,
        type:'POST',
        success:function(results) {
            $("#oggi1ora").attr("src", "grafici/oggi1ora.png?ts=" + d);
        }
    });
}

更新:如果你想每5秒调用一次/usr/local/bin/oggi1ora.php ,那么要么include '/usr/local/bin/oggi1ora.php' (或者你的目录结构已设置)或者添加exec('php -q /usr/local/bin/oggi1ora.php')在面向web的/var/www/oggi1ora.php脚本中。

暂无
暂无

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

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