我有这个php页面/var/www/oggi1ora.php.
首先,我需要执行/usr/local/bin/oggi1ora.php页面以生成图表.
每5秒钟php脚本会正确执行,但页面中的图像不会刷新…
我该如何解决?
我需要每5秒执行一次/usr/local/bin/oggi1ora.php.该脚本生成存储在/var/www/grafici/oggi1ora.png中的图像.
function refresh_div() {
jQuery.ajax({
url:'oggi1ora.php',
type:'POST',
success:function(results) {
jQuery(".result").html(results);
}
});
}
t = setInterval(refresh_div,5000);
这是新的/var/www/oggi1ora.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');
?>
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);
解决方法:
您可能有浏览器缓存问题.一种解决方案是在URL上附加时间戳,以确保获取最新版本.您可以将其设置为不缓存服务器端头.对于前一种解决方案,您可以尝试以下操作:
function refresh_div() {
var d = new Date().getTime();
jQuery.ajax({
url:'oggi1ora.php?ts=' + d,
type:'POST',
// rest of your code
对于后者,您可以这样设置标题:
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,则可以添加’/usr/local/bin/oggi1ora.php'(或者设置目录结构)或添加exec(面向网络的/var/www/oggi1ora.php脚本中的“ php -q /usr/local/bin/oggi1ora.php”).
标签:javascript,php,jquery
来源: https://codeday.me/bug/20191120/2041482.html