如何使用随机令牌重新加载带有 AJAX 的 PHP 脚本,URL 正在缓存


how to use a randomized TOKEN to reload PHP script with AJAX, url is being cached?

我正在做这个AJAX调用。如何使此 JQuery 刷新/重新加载正在调用的脚本并在之后执行 AJAX 调用。我想使用它,以防用户再次重新单击同一按钮并显示新数据。

问题:

如何包含令牌?我的show_aht.php正在缓存

单击aht_button时,它会返回数据,但是如果我刷新页面和/或重新单击按钮,它仍然会显示旧数据或不执行任何操作。我必须在浏览器上手动刷新"show_aht.php",然后单击"aht_button",以便我可以显示从"show_aht.php"检索的新数据。

我不想发布我的PHP代码,因为它有很多东西......也许有人可以找到问题,因为我不知道。不确定我们是否可以自行重新加载 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>

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

你可以添加你的成功:location.reload(); 自动刷新页面

或者你可以使用.on('click',function...),它将显示未来元素的更新结果