0

我正在使用 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

如何在操作中获取序列化数据?如何反序列化动作/控制器中的数据?

4

1 回答 1

1

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

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

这样,您getData()将以预期的格式返回数据。

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

于 2017-06-07T07:25:49.333 回答