I’ve got a GridView in my yii2 project and I want to make the list of rows sortable

Please note that I am doing this with JavaScript and Jquery, I did not download any 3rd party extensions. And I’ve actually done this before so I’m not really sure why it’s not working.

This is my JS code:

<script>
$(document).ready(function() {
    $('tbody').sortable({
        items: 'tr',
        cursor: 'move',
        update: function(event, ui) {
            var ids = [];
            $('tbody tr').each(function() {
                var id = $(this).data('key');
                if(id !== undefined && id !== null && id !== '') {
                    ids.push(id);
                }
            });
            console.log(ids);

            $.ajax({
                type: 'POST',
                url: '/index.php?r=admin/case/updateorder&ids=' + ids + '',
                data: { ids: ids, _csrf: '<?= Yii::$app->request->csrfToken ?>' },
                success: function (data) {
                    // Handle success if needed
                },
                error: function () {
                    // Handle error if needed
                }
            });
        }
    });
});

</script>

And this is the function in my controller:

public function actionUpdateorder($ids)
{
    $array = explode(",", $ids);
    $count = count($array);
    $i = 1;
    if (isset($data['ids'])) {
        $ids = $data['ids'];
        $array = explode(",", $ids);
        $count = count($array);
        $i = 1;

        foreach($array as $id) {
            $model = CaseTask::findOne($id);
            if ($model !== null) {
                $model->order = $i;
                $model->save();
                $i++;
            }
        }

        Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
        return ['success' => true];
    } else {
        Yii::$app->response->setStatusCode(400);
        return ['error' => 'Invalid request. Missing ids parameter.'];
    }
}

The sort functionality in the gridview actually works perfectly but everytime I sort I am getting this error:

Invalid Argument – yiibaseInvalidArgumentException
Response content must not be an array.

Because of it my sort data in my order column in my db is not saving. Anyone know what the problem is??

I tried transferring my data with JSON but that always returns null for me for some reason that is why i’ve resorted to trying to send my array through the URL, but I think that is the problem.