I’m having trouble accessing session data that was processed via PJAX / AJAX.
In one instance of this I have flash messages being set in my model that get triggered via a PJAX / AJAX action. However, the flash messages don’t show until a full page refresh. How can I show a flash message was set via a PJAX / AJAX action without refreshing the page?
public function actionDelete($id)
{
//delete your record
Yii::$app->session->setFlash('success', 'Record has been successfully deleted!');
}
// I have base AppController (parent controller) or you have put this method in each controller where you want to display messages.
public function afterAction($action, $result)
{
if (Yii::$app->request->isAjax && !empty(Yii::$app->session->getAllFlashes())) {
echo Alert::widget();
}
return parent::afterAction($action, $result);
}
Javascript:
In javascript method when you do the request:
$.post(url, function(data) {
// your code
$('#system-messages').html(data).stop().fadeIn().animate({opacity: 1.0}, 4000).fadeOut('slow');
});