1

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.

Msquare
9,4627 gold badges30 silver badges71 bronze badges
asked Nov 21, 2017 at 9:43

2 Answers 2

1

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
 }]
 });
 }
}
answered Nov 10, 2019 at 18:07
0

Try remove line from your code:

$block->setMessages($messageManager->getMessages(true));

I don't know why you need it. Removing it should help.

answered Nov 21, 2017 at 10:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.