我正在做这个AJAX调用。如何使此 JQuery 刷新/重新加载正在调用的脚本并在之后执行 AJAX 调用。我想使用它,以防用户再次重新单击同一按钮并显示新数据。
问题:
单击aht_button时,它会返回数据,但是如果我刷新页面并重新单击该按钮,它仍会显示旧数据。我必须在浏览器上手动刷新"show_aht.php",然后单击"aht_button",以便我可以显示从"show_aht.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",
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>
您可以使用
url 哈希进行重定向,并在页面重新加载后捕获它们
window.location = window.location + "#do=ajax";
// after page load
$(document).ready(function () {
var hash = window.location.toString().split("#")[1];
// cases
});