导航回上一页时,Ajax调用无法正常工作

时间:2014-04-26 09:52:57

标签: javascript php jquery ajax caching

我在php中有一个页面,我在使用ajax调用滚动到底部时加载了一些div 但是当我点击任何div进入下一页时,当我从导航页面返回时,我的ajax滚动将内容加载到窗口底部不起作用。 以下是我的代码

<?php
    $mysqli = new mysqli('localhost', 'root', '', 'table1');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      }
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Load data after page scroll </title>
<script type='text/javascript' src='js/jquery-1.7.2.min.js'></script>
<link rel="stylesheet" href="css/style.css" type="text/css" />

<script type="text/javascript" language="javascript"> 

$(document).ready(function(){

$(window).scroll(function(){
    scrollMore();
});

function scrollMore(){
    if($(window).scrollTop() == ($(document).height() - $(window).height())){

    var offset = $('[id^="myData_"]').length; // here we find out total records on page.
    var records = $(".allData").text();

    $(window).unbind("scroll");
        if(records != offset){
            $("#loaderImg").html('<img src="images/ajax-loader.gif" />');
            $("#loaderImg").show();
            loadMoreData(offset);
        }
    }
}

function loadMoreData(offset){
    $.ajax({
        type: 'get',
        async:false,
        url: 'getMoreData.php',
        data: 'offset='+offset,
        success: function(data){
        console.log(data);
            $("#loaderImg").empty();
            $("#loaderImg").hide();
            $(".loadData :last").after(data);
        },
        error: function(data){
            alert("ajax error occured…"+data);
        }
    }).done(function(){
        $(window).bind("scroll",function(){
        scrollMore();   
    });
    });
}


});
</script>
</head>
<body>
<?php
    $sql ="select * from xml";
    $countsql3 = mysqli_query($mysqli, $sql) or die("Cannot Get Pname Info: (".mysql_error().")");
    $numrows = mysqli_num_rows($countsql3);

    $limit = 20;
    $offset = 0;
    $mysql = "select * from xml limit ".$offset.", ".$limit."";
    $countsql3 = mysqli_query($mysqli, $mysql) or die("Cannot Get Pname Info: (".mysql_error().")");


?>

<div class="totalData">Total Records Found:<span class="allData"><?php echo $numrows;?></span></div>

<div class="mainDiv">
<div class="tableRow">
<div class="firstColumn"><strong>#</strong></div>
<div class="secondColumn"><strong>Record</strong></div>
</div>
<?php
$i = 1;
while($result = mysqli_fetch_array($countsql3)){ ?>
<div class="tableRow loadData" id="myData_<?php echo $i;?>">
<div class="firstColumn"><a href="nextpage.php"><?php echo $result['PID']; ?></a></div>
<div class="secondColumn"><?php echo $result['PID']?></div>
</div>
<?php $i++; }?>
<div class="tableRow">
<div class="secondColumn" id="loaderImg" style="display:none;"></div>
</div>
</div>



</body>
</html>

getMoreData.php

<?php
$mysqli = new mysqli('localhost', 'root', '', 'table1');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      }

$offset = (isset($_REQUEST['offset']) && $_REQUEST['offset']!='') ? $_REQUEST['offset'] : '';
$limit = 10;
$qry1 ="select * from xml limit ".$offset.", ".$limit."";
$countsql3 = mysqli_query($mysqli, $qry1) or die("Cannot Get Pname Info: (".mysql_error().")");
$i = ++$offset;
while($result = mysqli_fetch_array($countsql3)){ ?>
<div class="tableRow loadData" id="myData_<?php echo $i;?>">
<div class="firstColumn"><a href="nextpage.php"><?php echo $result['PID']; ?></a></div>
<div class="secondColumn"><?php echo $result['PID']?></div>
</div>
<?php $i++; } ?>

nextpage.php

<html>
<head>
</head>
<body>
hi 
</body>
</html>

请查看代码并指导我寻求解决方案

1 个答案:

答案 0 :(得分:0)

在您的Ajax调用集缓存为false时,可能是浏览器正在缓存调用,因为它是相同的而不是实际雇用它。禁用ajax缓存会强制它每次都发送一个新请求。

function loadMoreData(offset){
$.ajax({
    type: 'get',
    async:false,
    url: 'getMoreData.php',
    cache: false,
    data: 'offset='+offset,
    success: function(data){
    console.log(data);
        $("#loaderImg").empty();
        $("#loaderImg").hide();
        $(".loadData :last").after(data);
    },
    error: function(data){
        alert("ajax error occured…"+data);
    }
}).done(function(){
    $(window).bind("scroll",function(){
    scrollMore();   
});
});

另一种方法是在ajax函数中添加一个强制方法获取当前日期的变量,所以:

function loadMoreData(offset){

var dateNow = new Date().getTime() + Math.random();

$.ajax({
    type: 'get',
    async:false,
    url: 'getMoreData.php',
    cache: false,
    data: {offset: offset, time: dateNow }, 
    success: function(data){
    console.log(data);
        $("#loaderImg").empty();
        $("#loaderImg").hide();
        $(".loadData :last").after(data);
    },
    error: function(data){
        alert("ajax error occured…"+data);
    }
}).done(function(){
    $(window).bind("scroll",function(){
    scrollMore();   
});
});