Yii2 display format for JSON

code:

public function actionTest()
{
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ['test' => 1];
}

Answer:

<?php{"test":1}

I do not understand why <\? php "adds the answer. I'm trying to use:

Yii::$app->response->format = Response::FORMAT_RAW;

and

return Json::encode(['test' => 1]);

but it didn’t help me

+4
source share
1 answer

You should echoget the result. Here is the correct function:

public function actionTest()
{
    Yii::$app->response->format = Response::FORMAT_JSON;//optional
    echo Json::encode(['test' => 1]);
    return;
}
0
source

Source: https://habr.com/ru/post/1606167/


All Articles