Error in accessing post json data in yii2

ぃ、小莉子 提交于 2019-12-04 14:21:35

You are sending your JSON as encoded (post) data body, not key value pairs. So your approach is not working this way.

There are two options to fix this:

  1. refactor your controller into a RESTful service
  2. in your controller use the JSON body rather than POST parameters

While the first option is preferred in the long run, the second option is pretty simple as a quick fix.

First, make sure you configure your app to parse JSON body conten. IN config.php add this to the components array:

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

Then in your controller use this to get the JSON parameters:

$model->load(Yii::$app->getRequest()->getBodyParams());

I'm a newbie.. But I also want use checkboxcoloumns in gridview (Kartik version). 1st thing.

Instead of writing

var keys = $('#grid').yiiGridView('getSelectedRows');

I have to write

var keys = $('#w4').yiiGridView('getSelectedRows');

2nd thing. In the controller you can process the keylist, but don't try to decode it, simple use it int this way:

        $keys = $_POST['keylist'];

and it seems it works for me!

Sorry for my english..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!