M
M
Maxim2018-06-12 17:19:14
Yii
Maxim, 2018-06-12 17:19:14

What's wrong with select2 ajax?

Mistake

{"more":false,"results":{"id":0,"text":"Ничего не найдено"}}
An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /Users/maksimvorozcov/Web/eventdance/backend/modules/profile/controllers/DefaultController.php on line 192. in /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2 /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /Users/maksimvorozcov/Web/eventdance/backend/modules/profile/controllers/DefaultController.php on line 192. in /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /Users/maksimvorozcov/Web/eventdance/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#2 /Users/maksimvorozcov/Web/eventdance/backend/web/index.php(36): yii\base\Application->run()
#3 {main}

Controller
public function actionDancer() {

        $search = Yii::$app->request->post('search')['term'];
        $out = ['more' => false];
        if (!is_null($search)) {

            $query = Profile::find()
                ->orderBy('name')
                ->andFilterWhere(['like', 'name', $search])
                ->all();

            foreach ($query as $i => $data) {
                $out['results'][$i] = ['id' => $data->id, 'text' => $data->getFullName()];
            }

        } else {
            $out['results'] = ['id' => 0, 'text' => 'Ничего не найдено'];
        }
        echo Json::encode($out);
    }

view
<?= $form->field($model, 'user_id')->widget(Select2::classname(), [
        'id' => 'user',
        'options' => [
            'placeholder' => 'Выберите танцора...',
            'class' => 'form-control',
        ],
        'initValueText' => !empty($model->user_id) ? $model->profile->getFullName() :'',
        'pluginOptions' => [
            'allowClear' => true,
            'minimumInputLength' => 1,
            'ajax' => [
                'url' => Url::to(['/profile/default/dancer']),
                'dely' => 250,
                'type' => 'post',
                'dataType' => 'json',
                'data' => new JsExpression('function(term,page) { return {search:term}; }'),
                'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),
            ],
        ],
    ]);?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-06-12
@myks92

one.

An Error occurred while handling another error
- most often a sign that there was an attempt to display an error, but an error occurred in the process. Apparently there is a problem with the errorHandler
2 settings. The action must return something.
why
not do it instead
Yii::$app->response->format = Response::FORMAT_JSON;
return $out;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question