简体   繁体   中英

Javascript's ajax won't run on browser's back button

My javascript ajax script seems to not work whenever a page was loaded through the back button or forward button of the browser

//
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');
    }
  });
}

i am triggering the event by

$(document).ready( function(){
    IncreasePopularity();
});

the script works on refresh or when the page has been loaded normally (by click the button to enter the page) and it also updates the database but when the page was loaded through the back or forward of the browser it only alerts 'haha' but it doesn't increase the database

the answer is to just add the cache:false, into the ajax script

//
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');
    }
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM