1

在我的索引页面上,我的表格的每一行都有复选框。

要编辑选定的项目,我使用 javascript/jquery 来获取复选框的类,构建一个 id 数组,然后将其发布到我的控制器中的编辑选定方法。

现在,这一切都运行良好,但是当在我的 App Controller 中启用安全性时,我的帖子会被黑洞,并且不会发布数组。

这是我的 index.ctp 文件:

<table id="indexTable">
    <thead><tr>
        <th> <?php echo $this->Form->checkbox('select_all', array('value' => 'select_all')); ?> </th>
        <th> <?php echo $this->Paginator->sort('id', 'ID'); ?> </th>
        <th> <?php echo $this->Paginator->sort('name', 'Name'); ?> </th>
        <th>Auto Offset  </th>  <th>UTC Offset Sec  </th>  <th>In Month  </th>
        <th>In Week      </th>  <th>In Dow          </th>  <th>In Hour   </th>  <th>Out Month   </th>
        <th>Out Week     </th>  <th>Out Dow         </th>  <th>Out Hour  </th>  <th>Offset Sec  </th>
        <th>DST Ref      </th>  <th>Actions      </th>
    </tr></thead>

    <tbody>

<?php
$this->Form->create('LocalClock');
foreach($localClocks as $LocalClock) { ?>
        <tr>
            <td>  <?php echo $this->Form->checkbox('LocalClocks'.$LocalClock['LocalClock']['id'], array('value' => $LocalClock['LocalClock']['id'], 'hiddenField' => false));?>  </td>
            <td>  <?php echo $LocalClock['LocalClock']['id']; ?>              </td>
            <td>  <?php echo $LocalClock['LocalClock']['name']; ?>            </td>
            <td>  <?php echo $LocalClock['LocalClock']['auto_offset']; ?>     </td>
        </tr>
<?php } ?>
    </tbody>
</table>

</div>

<!-- This <div> contains all the actions that can be performed on the Local Clocks. -->
<div>
    <p>

        <span style="float: left">
            <?php echo $this->Html->link(__('Edit All Items'), array('action' => 'editAll'), array('class' => 'link'));?> &nbsp;&nbsp;
        </span>

        <span style="float: left">
            <?php echo $this->Html->link(__('Edit Selected Items'), array('action' => 'lceditSelected'), array('class' => 'general_dialog'));?> &nbsp;&nbsp;
        </span>

        <span style="float: right">
            <?php echo $this->Html->link(__('Delete Selected Items'), array('action' => 'deleteSelected'), array('class' => 'general_dialog'));?> &nbsp;&nbsp;
        </span>
    <?php $this->Form->end(); ?>

    </p>
</div>

我拿出了一些不重要的东西。问题在于我选择的编辑和删除选定的功能。

这是等待他们点击然后构建数组以发布到控制器操作的 javascript 代码:

$('.general_dialog').live('click', function()
{
    $.ajaxSetup({ async: false });

        var $selDialog = $("#general_dialog").dialog(
        {
            autoOpen: false,
            modal: true,
        });

        var postInfo = $('#LocalClockIndexForm').serialize();
        $.ajax({
        url: $(this).attr('href'),
        type: "post",
        data: postInfo,
        success: function (response)
        {
            alert('success');
        },
        error: function()
        {
            alert("failed");
        }
    });
        $selDialog.load($(this).attr('href'), function ()
        {
            $selDialog.dialog('open');
        });
    return false; // Ensure the controller does not redirect to the actual edit page
});

任何关于如何让它工作而不被黑洞的帮助将不胜感激。

提前致谢

- - - - - - - - - - - - - - - - - - - - - 编辑 - - - - --------------------------------------- 我将$this->Form->create('LocalClock')and添加$this->Form->end()到表中并切换了 $ .post() 和 $.ajax() 调用。

如果我发送序列化表单,我不会得到黑洞,但是当我查看发布的数据时,它不包含任何复选框 ID。

4

2 回答 2

2

您的示例缺少表单标签。

您需要使用 Form helper 创建和结束表单,以确保安全令牌包含在表单生成中:

$this->Form->create();
  // Other form elements here.
$this->Form->end();
于 2012-07-28T10:04:22.820 回答
1

您是否尝试过修改 crsf 保护策略?如果您使用相同的令牌重新加载页面,安全组件将黑洞请求。

var $components = array('Security'=>array('csrfUseOnce'=>false));
于 2012-10-10T09:27:34.603 回答