在 cakephp 3 中使用 getData 访问 jquery 反序列化数据

access jquery unserialized data using getData in cakephp 3

我正在使用 cakephp 3.4

我有一个使用 ajax 提交值的表单。

<?= $this->Form->create(null, ['id' => 'search-form']) ?>
<?= $this->Form->control('keyword') ?>
<?= $this->Form->button(__('Search'), ['id' => 'search-submit']); ?>
<?= $this->Form->end() ?>

并使用

将此数据发送到操作
$('#search-submit').click(function(event){
    event.preventDefault();
    $.post('/dashboard/custom-search/ajax-search',
    {
        data: $('#search-form').serialize()
    }, function (response)
    {
        $('#search-result').html(response);
    });
    return false;
});

在我调试请求数据时的ajaxSearch动作中

debug($this->request->getData());

它给出

[
    'data' => '_method=POST&keyword=world'
]

但是当我尝试

debug($this->request->getData('keyword'));

给出

null

如何在操作中获取序列化数据?如何在action/controller中反序列化数据?

您需要更改的是将序列化数据发布到的方式:

$.post('/dashboard/custom-search/ajax-search',
    $('#search-form').serialize(),
    function (response){
        $('#search-result').html(response);
});

这样您的 getData() 将 return 数据以预期的格式显示。

有关通过 jQuery.post() 传递序列化数据的完整信息可在此处找到:jQuery.post()