In Magento 2.3.3, I save form data by ajax and need to display success message after save records.

Expected Result :

Message manager should be display.

Actual Result :

Message manager not display. It will display after page reload.

Controller code :

 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
namespace Vendor\Magento\Controller\Adminhtml\Post;

class Save extends \Magento\Backend\App\Action {

     * @var \Magento\Framework\View\Result\PageFactory
    protected $resultPageFactory;

     * @var \Magento\Backend\Model\Session
    protected $adminsession;

     * @param \Magento\Backend\App\Action\Context        $context           [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Backend\Model\Session $adminsession
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->adminsession = $adminsession;

    public function execute() {
        $resultRedirect = $this->resultRedirectFactory->create();
        /** Save Logic */
        try {
                $this->messageManager->addSuccessMessage(__('Data has been saved.'));
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
            } catch (\RuntimeException $e) {
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));

Note : I don't want to set message using jQuery.

Any help would be appreciated.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.