captchaHelper = $captchaHelper; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); parent::__construct($context); } /** * {@inheritdoc} */ public function execute() { $formId = $this->_request->getPost('formId'); if (null === $formId) { $params = []; $content = $this->_request->getContent(); if ($content) { $params = $this->serializer->unserialize($content); } $formId = isset($params['formId']) ? $params['formId'] : null; } $captchaModel = $this->captchaHelper->getCaptcha($formId); $captchaModel->generate(); $block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName()); $block->setFormId($formId)->setIsAjax(true)->toHtml(); $this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()])); $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true); } }