AJAX和PHP:如何停止脚本缓存? [英] AJAX and PHP: how to stop script being cached?

查看:90
本文介绍了AJAX和PHP:如何停止脚本缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

如何添加令牌?我的show_aht.php已被缓存.我必须手动刷新show_aht.php才能在进入aht_button进行AJAX调用时在map.php中获取新数据.它非常令人沮丧.

How do I include a token? My show_aht.php is being cached. I have to refresh manually my show_aht.php in order to get new data in my map.php when I get on the aht_button to make the AJAX call. Its very frustrating.

单击aht_button时,它将返回数据,但是如果刷新页面和/或重新单击该按钮,它将仍然显示旧数据或什么都不做.我必须在浏览器中手动刷新"show_aht.php",然后单击"aht_button",这样我才能显示从"show_aht.php"中检索到的新数据.

When aht_button is clicked it returns data, but if I refresh the page and/or I reclick the button it will still show me the old data or do nothing at all. I have to manually refresh my "show_aht.php" on my browser and then click on "aht_button" so I can display the new data being retrieve from "show_aht.php".

我不想发布我的PHP代码,因为它有很多东西.也许有人可以找到问题,因为我不知道.不知道我们是否可以自己重新加载PHP脚本?我只放了重要的东西.

I did not want to post my PHP code because its a lot of stuff.. maybe someone can find the problem because I have no clue. Not sure if we can reload a PHP script by itself? I've put only the important stuff.

提前谢谢!

map.php JS:

map.php JS:

<div id="aht">
    <button id="aht_button">AHT</button>    
</div>

<script type="text/javascript">
        $(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"GET",
                    url : "show_aht.php", //use a token here??
                    data:{ } ,
                    dataType: 'json',
                    success : function(data){
                        //get the MIN value from the array
                            var min = data.reduce(function(prev, curr) {
                                return isNaN(+curr['aht_value']) || prev < +curr['aht_value'] ? prev : +curr['aht_value'];
                            }, 1000000);

                            alert("min:" + min); 
                            //get the MAX value from the array
                            var max = data.reduce(function(prev, curr) {
                              return isNaN(+curr['aht_value']) || prev > +curr['aht_value'] ? prev : +curr['aht_value'];
                            }, -1000000); 

                            alert("max:" + max);
                            //function for calculation of background color depending on aht_value               
                            function conv(x){
                                return Math.floor((x - min) / (max - min) * 255);
                            }

                            //function for background color, if NA then show white background, either show from green to red
                            function colorMe(v){
                              return v == 'NA' ? "#FFF" : "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
                            }

                        //going through all DIVs only once with this loop
                        for(var i = 0; i < data.length; i++) { // loop over results
                        var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
                        if(divForResult.length) { // if a div was found
                            divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
                        }//end if
                        }//end for  
                    }//end success
                });//end ajax   
              });//end click
            });//end rdy
        </script>

show_aht.php:

show_aht.php:

include 'db_conn_retca2003.php';
include 'db_conn_retca2001.php';
header('Content-type: application/json');

    /****************************************************
    matching USER array and MEMO array 
    for matching username values
    /****************************************************/
    $result = array();
    foreach ($memo as $username => $memodata) {
    if (in_array($username, array_keys($user))) {
    // Match username against the keys of $user (the usernames) 
    $userdata = $user[$username];
    //if AHT is null give N/A as value
    if (is_null($memodata['aht_value'])) {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => 'NA',
                                             'station'  => $userdata['station']
                                            );
    }//end inner if 
    //else give the actual value of AHT without the decimals
    else {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => substr($memodata['aht_value'],0,-3),
                                             'station'   => $userdata['station']
                                        );
    }//end else
    }//end outer if
    }//end for
  echo json_encode($result);
?>

推荐答案

可以通过多种方式停止浏览器缓存.一种是发送指示不进行缓存的标头.来自如何在所有页面上控制网页缓存浏览器?:

There's multiple ways you can stop browser caching. One is to send headers that indicate no caching. From How to control web page caching, across all browsers?:

适用于所有提到的标题的正确最小标题集 浏览器:

The correct minimum set of headers that works across all mentioned browsers:

Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 

使用PHP:

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1. 
header('Pragma: no-cache'); // HTTP 1.0. header('Expires: 0'); // Proxies.

使用HTML:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> 
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

请注意,此方法取决于浏览器是否遵守no缓存标头.这不是保证.

Note that this method depends on browsers respecting the no cache headers. This is not a guarantee.

当链接到您不希望被缓存的文件时,您还可以添加一个包含当前时间戳的查询字符串变量.由于您每次都将使用不同的URL,因此浏览器将不会缓存.

You can also add a query string variable that contains the current time stamp when linking to those files you do not wish to be cached. Since you're going to a different URL every time, the browser will not cache.

这篇关于AJAX和PHP:如何停止脚本缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆