myks92
@myks92
Нашёл решение — пометь вопрос ответом!

В чем ошибка select2 ajax?

Ошибка
{"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}


Контроллер
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}; }'),
            ],
        ],
    ]);?>
  • Вопрос задан
  • 170 просмотров
Решения вопроса 1
webinar
@webinar Куратор тега Yii
Учим yii: https://youtu.be/-WRMlGHLgRg
1.
An Error occurred while handling another error
-чаще всего признак того, что была попытка вывести ошибку, но в процессе этого произошла ошибка. Видимо проблема с настройками errorHandler
2. экшен должен что-то возвращать.
почему бы вместо
echo Json::encode($out);
не сделать
Yii::$app->response->format = Response::FORMAT_JSON;
return $out;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы