Javascript的ajax不会在浏览器的后退按钮上运行

时间:2018-01-01 15:56:34

标签: javascript ajax

当通过浏览器的后退按钮或前进按钮加载页面时,我的javascript ajax脚本似乎无法正常工作

//
function IncreasePopularity(){
  var hiddenid = <?php echo $id; ?>; //$id is the id of the user being rated
  $.ajax ({
    url: 'IncreasePopularity.php',
    data: 'id='+hiddenid,
      success: function(data){
     alert('haha');
    }
  });
}

我正在通过

触发事件
$(document).ready( function(){
    IncreasePopularity();
});

脚本适用于刷新或页面正常加载(通过单击按钮进入页面),它也会更新数据库,但是当页面通过浏览器的后面或前面加载时,它只会提醒和#39;哈哈&#39;但它并没有增加数据库

1 个答案:

答案 0 :(得分:0)

答案是将缓存:false添加到ajax脚本

//
function IncreasePopularity(){
  var hiddenid = <?php echo $id; ?>; //$id is the id of the user being rated
  $.ajax ({
    url: 'IncreasePopularity.php',
    data: 'id='+hiddenid,
    cache: false,
      success: function(data){
     alert('haha');
    }
  });
}