The controller takes the Ajax request and after some required operations should return the HTML of the notification message using message manager so that it can be rendered on the front end using Javascript.
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$messageManager = $_objectManager->create('\Magento\Framework\Message\ManagerInterface');
$block = $this->_layoutFactory->create()->getMessagesBlock()->addError('Configuration is wrong!');
$block->setMessages($messageManager->getMessages(true));
$resultJson = $this->resultJsonFactory->create();
$mess= $block->getGroupedHtml();
return $resultJson->setData([
'messages' => $mess,
'error' => '$error value comes here'
]);
But function getGroupedHtml() does not return any html.
Being a beginner to Magento, my queries are
What I am trying to do is feasible? If yes, then what have missed I or done wrong?
Thanks.
2 Answers 2
Here It is Hope this will help You
define ([
'jquery',
'Magento_Customer/js/customer-data'
], function (,ドル customerData) {
'use strict';
return function () {
var msg = 'some message here';
customerData.set('messages', {
messages: [{
type: 'success',
text: msg
}]
});
}
}
Try remove line from your code:
$block->setMessages($messageManager->getMessages(true));
I don't know why you need it. Removing it should help.