我以前从未编写过 Ajax,目前我正在尝试编写一个无限滚动页面。当用户向下滚动到页面底部时,应该加载更多项目,但现在它们没有加载。这是我的 Javascript,用于检测它们何时触底并进行 Ajax 调用:
window.onload=function(){
//Find out how many items I have loaded and what filter I am using so I can make the Ajax call
var vloaded = <?php echo $i; ?>;
var vfilter = "<?php echo $filter ?>";
$(window).on('scroll', function () {
if ($(window).height() + $(window).scrollTop() >= $(document).height() - 10) {
//I have reached the bottom of the page, now load items
alert("loaded is " + vloaded + " and filter is " + vfilter);
$.post("/organizer/getMore",
{ filter: vfilter, loaded: vloaded },
function(responseText) {
$("grid").append(responseText);
},"html");
//I've loaded the next 30 items, increment my counter for next time
vloaded +=30;
}
});
}
当我触底时会显示警报,并且我的变量正在正确递增。我正在使用 Zend Framework,所以 URL 指向我的getMoreAction()
函数:
public function getmoreAction()
{
//Used by Ajax to get more items for the infinite scroll
//Figure out how I'm filtering items and how many I've already loaded
$filter = $_POST['filter'];
$loaded = $_POST['loaded'];
echo "Filter is ".$filter;
echo "Loaded ".$loaded;
//Get all the items in the database ordered by filter
require_once(APPLICATION_PATH . '/models/ItemArray.php');
$items = ItemArray::getItems($user->getID(), $filter, $loaded );
//Return items back to Ajax call, converted to html
echoItems($items);
}
我已经知道该getItems
功能有效,因为我也在页面首次加载时使用它,并且echoItems
只是一个循环来回显每个项目的 html,它也适用于其他地方。动作中的回声永远不会执行,所以我假设我的 post call 有问题,以至于我什至从未进行过这个动作。