Invalid JSON data

When I try to download a few lines, the following error occurs: Invalid JSON data. in D:\OSPanel\domains\mysite\vendor\yiisoft\yii2\helpers\BaseJson.php:102

My controller:

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['logout'],
                        'roles' => ['@'],
                    ],
                ],
            ],

            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                    'delete-all' => ['post'],

                ],
            ],
        ];
    }
……...
public function actionDownloadAll(){

        if (isset($_POST['keylist'])) {
            $keys = Json::decode($_POST['keylist']);
            foreach ($keys as $id) {
                $model = $this->findModel($id);
                $files = $model->file;
                $path = Yii::getAlias('@app/web/file') ;
                $file = $path . '/' . $files;
                if (file_exists($file)) {
                    return Yii::$app->response->sendFile($file);
                }
                throw new Exception('Файл не найден');

            }
        }
        $this->redirect(['index']);
    }

and view:

<?= Html::beginForm(['delete-all']) ?>

    <p>br>
        <?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
        <?= Html::submitButton('Удалить выбранные', ['class' => 'btn btn-success']); ?>
        <?= Html::a('Скачать выбранные', ['download-all'], ['id' => 'MyButton', 'class' => 'btn btn-success']) ?>  </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => 
            ['class' => 'yii\grid\CheckboxColumn',
                'headerOptions' => ['style' => 'text-align: center;'],
                'contentOptions' => ['style' => 'text-align: center;'],
                'checkboxOptions' => function ($model, $key, $index, $column) {
                    return ($model->name == 'hgh')?['checked'=>"checked"]:[];     }
            ],

            ['class' => 'yii\grid\SerialColumn',
                'header' => '№',
            ],
            'name',
            'doc_num',
            

            ['class' => 'yii\grid\ActionColumn',
                'contentOptions' => ['style' => 'text-align: center;'],
                'header' => 'Действия',
                'headerOptions' => ['width' => '70'],
                'template' => '<div>&nbsp{update} {delete} {download}</div>'],
        ],
    ]);
    ?>
    <?php
    $this->registerJs(
        "$(document).ready(function(){
            $('#MyButton').click(function(){
                var keys = $('#w0').yiiGridView('getSelectedRows');
                $.ajax({
                    type: \"POST\",
                    url: \"download-all\",
                    data: {keylist: keys},
                    dataType: \"json\",
                });
            });
        });",
\yii\web\View::POS_READY);
?>
<?= Html::endForm(); ?>

Ответы (0 шт):