Pjax with flash messages usage

Is there way to set flash message in controller and get it from the pjax response?

I think the simpler way is to set the message:


\Yii::$app->getSession()->setFlash('msg', 'Some message....');

And get the message in another action like below:


$msg = \Yii::$app->getSession()->getFlash('msg');

return json_encode($msg);