Hi guys,
I’m using following code in order to get content of table using Ajax-JSON and kartik widget Select2. Unfortunately, I will get error 403 and hint of strict-origin-when-cross-origin. What’s wrong with this code?
Controller:
public function actionAuswahl($q = null, $id = null) {
$out = ['results' => ['id' => '', 'text' => '']];
if (is_null($q)) {
$query = new \yii\db\Query;
$query->select('id, plz AS text')
->from('l_plz')
->where(['like', 'plz', $q]);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
} elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => \backend\models\LPlz::find($id)->plz];
}
Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP;
return $out;
}
View:
<div class="col-md-4">
<?=
$form->field($model, 'l_plz_id', ['addon' => [
'prepend' => ['content' => 'Plz']]])->widget(\kartik\widgets\Select2::classname(), [
'data' => \yii\helpers\ArrayHelper::map(\backend\models\LPlz::find()->orderBy('id')->asArray()->all(), 'id', 'plz'),
'options' => [
'placeholder' => 'PLZ selektieren',
'id' => 'zip_code'
],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'language' => [
'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
],
'ajax' => [
'url' => Url::to(['/pa-patient/auswahl']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(data) { console.log(data.ort); return data.ort; }'),
'templateSelection' => new JsExpression('function (data) { return data.ort; }'),
],
]);
?>
</div>