PHP从jQuery空发送时未接收到POST数组

时间:2019-01-28 06:52:39

标签: php jquery ajax laravel

我正在使用Laravel。当我dd($request->all())时,仅当数组不为空时即Array['some-value']才输出数据。当我在jQuery中console.log(data)时,它登录控制台是一个数组,但是里面没有任何值。但是在Laravel中,我无法获取该空数组,但是当它具有值时可以获取它。我想发送数组,即使它为空。

我该怎么做?

更新

HTML:

<ul>
    <!-- It sends but doesn't show in Laravel when this active class below is not exists -->
    <li class="active" data-motion="1">motion 1</li>
    <li data-motion="2">motion 2</li>
    <li data-motion="3">motion 3</li>
</ul>

jQuery:

$.each($('ul li.active'), function(index, motion) {
    motion = $(motion).attr('data-motion');
    params['motions'].push(motion);
});

// it shows the empty array when that active class is not there,
// which is the one I want to get in Laravel too.
consol.log(params);

$.ajax({
    url: '/my-motions',
    type: 'PUT',
    dataType: 'json',
    data: params,
    success: function(response) {
        params = [];
    },
    error: function(error) {
        //
    }
});

Laravel:

public function myFunc(Request $request)
{
    $data = $request->all();
    dd($data);
    // The result: nothing
    // What I want: array:0 [] 
}

0 个答案:

没有答案