首页 > 解决方案 > PHP从jQuery发送空时未接收POST数组

问题描述

我正在使用 Laravel。当我dd($request->all())只在数组不为空时打印数据,即Array['some-value']。当我console.log(data)在 jQuery 中时,它会在控制台中记录它是一个数组,但里面没有任何值。但是在 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) {
        //
    }
});

拉拉维尔:

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

标签: phpjqueryajaxlaravel

解决方案


推荐阅读