This problem is going to be a little harder to explain, so bear with me. I have created ajax pagination for my application. Here is the core code for how it functions in the controller (some code is obviously changed or not shown for privacy concerns):
function action(){
//PROBLEM LIES HERE
if ($this->params['isAjax']){
$page = $_COOKIE['page'];
} else {
$page = 1;
}
//END PROBLEM AREA
$offset = ($page - 1) * $this->perPage;
$limit = $this->perPage + 1; //+1 so we can test if there are more after
setcookie('page', ++$page, 0, '/');
$objects = $this->getObjects($query, $limit, $offset);
$moreObjects = count($objects) > $this->perPage;
$objects = array_slice($objects, 0, $this->perPage);
$this->set('objects', $objects);
if ($this->params['isAjax']){
$this->set('type', 'objects');
$rendered = $this->render('/elements/ajax_elements', null);
echo json_encode(array('objects' => $rendered, 'moreObjects' => $moreObjects));
$this->_stop();
}
$this->set('moreObjects', $moreObjects);
}
I have included the RequestHandler component. This code works mostly, the problem is that the AJAX request is not being set by CakePHP (v 1.3) after the second page. So consequently the the third page is the same page as the second page (duplicates), this happens since the $page gets set to 1 again. What is also weird, is that the following pages load fine. I have examined the HTTP requests, there is a clear difference between the AJAX requests and the non-AJAX requests and the second page loading problem request is most always a non-AJAX request. Even this code returns false on the second page:
function isAjax() {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");
}
Why is this happening? If you need me to provide any other information, let me know. I am happy to hear any suggestions, solutions and/or debugging ideas.
This doesn’t sound like a CakePHP issue, it sounds like the headers that your browser is sending with the request.
I would recommend using a request inspection tool, like Fiddler (http://fiddler2.com/fiddler2/). This will let you inspect the headers and the requests and responses from all of your calls.