Controller Action Should Return "application/json"

Hello,

how can I return "application/json" from my action and do not render a view?

Thanks,

Urkman

Two methods to do this

Method 1:

Set the following in your control action before you return a render.




\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;



Method 2:

Instead of returning a render view in your controller action do the following:




echo \yii\helpers\Json::encode($content);



Thanks :)

Useful stuff for me too. I was sending an AJAX response to the view and using ‘echo’ from there but I like this method better.