views:

97

answers:

0

I'm using CakePHP 1.3.2 with jQuery 1.4.2 and I'm seeing some strange behaviour with AJAX requests.

Intermittently, when clicking an AJAX pagination link I'll see 2 POST requests instead of 1, the first of which is via XMLHttpRequest, the second of which isn't. The first request never completes, but returns a full page using the default.ctp layout. The second request completes with a 200 OK response and also returns a full page wrapped in default.ctp

The HTML returned never gets displayed in the div it should be showing in and just hangs.

The above intermittent behaviour also happens with Ajaxy (an ajax framework) so it doesn't seem to be down to the javascript being used.

I read somewhere that CakePHP always responds with the default layout if there's an error, but there's no error message returned with debug set to 2 in core.php

After this issue, If I click on any other AJAX links they don't respond with new content. The entire page needs to be refreshed before AJAX links work again and they return content without the default.ctp layout.

controller:

<?php
class UsersController extends AppController {

    var $name = 'Users';

    var $helpers = array('Html', 'Form', 'Session', 'Js');
    var $components = array('Security', 'Session', 'RequestHandler');

    var $paginate = array(
        'limit' => 2,
        'order' => array('User.id' => 'asc')

    );

    function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }
}
?>

view:

<div class="users index">
<?php
$this->Paginator->options(array(
    'update' => '#content',
    'evalScripts' => true
));
?>


    <h2><?php __('Users');?></h2>
    <table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('id');?></th>
            <th><?php echo $this->Paginator->sort('username');?></th>
            <th><?php echo $this->Paginator->sort('password');?></th>
            <th><?php echo $this->Paginator->sort('created');?></th>
            <th><?php echo $this->Paginator->sort('modified');?></th>
            <th><?php echo $this->Paginator->sort('group_id');?></th>
            <th><?php echo $this->Paginator->sort('sms_identifier');?></th>
            <th><?php echo $this->Paginator->sort('company_name');?></th>
            <th><?php echo $this->Paginator->sort('notify_url');?></th>
            <th><?php echo $this->Paginator->sort('credits_remaining');?></th>
            <th><?php echo $this->Paginator->sort('user_status_id');?></th>
            <th><?php echo $this->Paginator->sort('company_registration_number');?></th>
            <th><?php echo $this->Paginator->sort('company_trading_name');?></th>
            <th><?php echo $this->Paginator->sort('company_type_id');?></th>
            <th><?php echo $this->Paginator->sort('receive_number');?></th>
            <th><?php echo $this->Paginator->sort('receive_url');?></th>
            <th><?php echo $this->Paginator->sort('receive_email');?></th>
            <th><?php echo $this->Paginator->sort('receive_url_content_type_override');?></th>
            <th><?php echo $this->Paginator->sort('receive_delivery_dll');?></th>
            <th><?php echo $this->Paginator->sort('parent_id');?></th>
            <th><?php echo $this->Paginator->sort('lft');?></th>
            <th><?php echo $this->Paginator->sort('rght');?></th>
            <th><?php echo $this->Paginator->sort('partner_id');?></th>
            <th><?php echo $this->Paginator->sort('internal_note');?></th>
            <th><?php echo $this->Paginator->sort('subpartner_id');?></th>
            <th class="actions"><?php __('Actions');?></th>
    </tr>
    <?php
    $i = 0;
    foreach ($users as $user):
        $class = null;
        if ($i++ % 2 == 0) {
            $class = ' class="altrow"';
        }
    ?>
    <tr<?php echo $class;?>>
        <td><?php echo $user['User']['id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['username']; ?>&nbsp;</td>
        <td><?php echo $user['User']['password']; ?>&nbsp;</td>
        <td><?php echo $user['User']['created']; ?>&nbsp;</td>
        <td><?php echo $user['User']['modified']; ?>&nbsp;</td>
        <td><?php echo $user['User']['group_id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['sms_identifier']; ?>&nbsp;</td>
        <td><?php echo $user['User']['company_name']; ?>&nbsp;</td>
        <td><?php echo $user['User']['notify_url']; ?>&nbsp;</td>
        <td><?php echo $user['User']['credits_remaining']; ?>&nbsp;</td>
        <td><?php echo $user['User']['user_status_id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['company_registration_number']; ?>&nbsp;</td>
        <td><?php echo $user['User']['company_trading_name']; ?>&nbsp;</td>
        <td><?php echo $user['User']['company_type_id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['receive_number']; ?>&nbsp;</td>
        <td><?php echo $user['User']['receive_url']; ?>&nbsp;</td>
        <td><?php echo $user['User']['receive_email']; ?>&nbsp;</td>
        <td><?php echo $user['User']['receive_url_content_type_override']; ?>&nbsp;</td>
        <td><?php echo $user['User']['receive_delivery_dll']; ?>&nbsp;</td>
        <td><?php echo $user['User']['parent_id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['lft']; ?>&nbsp;</td>
        <td><?php echo $user['User']['rght']; ?>&nbsp;</td>
        <td><?php echo $user['User']['partner_id']; ?>&nbsp;</td>
        <td><?php echo $user['User']['internal_note']; ?>&nbsp;</td>
        <td><?php echo $user['User']['subpartner_id']; ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View', true), array('action' => 'view', $user['User']['id'])); ?>
            <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $user['User']['id'])); ?>
            <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $user['User']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $user['User']['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </table>
    <p>
    <?php
    echo $this->Paginator->counter(array(
    'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
    ));
    ?>  </p>

    <div class="paging">
        <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
     |  <?php echo $this->Paginator->numbers();?>
 |
        <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
    </div>
</div>
<div class="actions">
    <h3><?php __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New User', true), array('action' => 'add')); ?></li>
    </ul>
</div>
<?php echo $this->Js->writeBuffer(); ?>

Default layout:

<?php
/**
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.view.templates.layouts
 * @since         CakePHP(tm) v 0.10.0.1076
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php __('CakePHP: the rapid development php framework:'); ?>
        <?php echo $title_for_layout; ?>
    </title>
    <?php
        echo $this->Html->meta('icon');

        echo $this->Html->css('cake.generic');

        echo $html->script('jquery-1.4.2');

        echo $scripts_for_layout;
    ?>
</head>
<body>
    <div id="container">
        <div id="header">
            <h1><?php echo $this->Html->link(__('CakePHP: the rapid development php framework', true), 'http://cakephp.org'); ?></h1>
        </div>
        <div id="content">

            <?php echo $this->Session->flash(); ?>

            <?php echo $content_for_layout; ?>

        </div>
        <div id="footer">
            <?php echo $this->Html->link(
                    $this->Html->image('cake.power.gif', array('alt'=> __('CakePHP: the rapid development php framework', true), 'border' => '0')),
                    'http://www.cakephp.org/',
                    array('target' => '_blank', 'escape' => false)
                );
            ?>
        </div>
    </div>
    <?php echo $this->element('sql_dump'); ?>
</body>
</html>

First request headers:

Response headers:
Content-Type text/html; charset=utf-8
Server Microsoft-IIS/7.5
X-Powered-By PHP/5.2.13, ASP.NET
P3P CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Date Thu, 14 Oct 2010 10:28:59 GMT
Content-Length 67467

Request Headers:
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0C) FirePHP/0.4
Accept text/plain, /
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer localhost/index.php/admin/users
Content-Length 0
Content-Type text/plain; charset=UTF-8
Cookie toolbarDisplay=show; BOOMSMS=fct4l1saj8dslvlop1puvnn5j6

Second request headers:

Response Headers:
Content-Type text/html; charset=utf-8
Server Microsoft-IIS/7.5
X-Powered-By PHP/5.2.13, ASP.NET
P3P CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Date Thu, 14 Oct 2010 10:28:59 GMT
Content-Length 67467

Request Headers:
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0C) FirePHP/0.4
Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Length 0
Content-Type text/plain; charset=UTF-8
Referer localhost/index.php/admin/users
Cookie toolbarDisplay=show; BOOMSMS=fct4l1saj8dslvlop1puvnn5j6