How to create block from controller by ajax method?
$result = $this->resultJsonFactory->create();
$resultPage = $this->_resultPageFactory->create();
$block = $resultPage->getLayout()
->createBlock('Module\Ajaxcart\Block\BuildProductInfo')
->setTemplate('Module_Ajaxcart::buildproductinfo.phtml')
->toHtml();
Showing Cache Exception Error
asked May 28, 2019 at 12:00
Masud Shaikh
1,19922 silver badges55 bronze badges
2 Answers 2
Try This :-
protected $resultPageFactory;
public function __construct(
Magento\Framework\View\Result\PageFactory $resultPageFactory
){
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
$layout = $this->resultPageFactory->create();
$block = $layout->getLayout()
->createBlock('Module\Ajaxcart\Block\BuildProductInfo')
->setTemplate('Module_Ajaxcart::buildproductinfo.phtml')
->toHtml();
}
answered May 28, 2019 at 12:06
Ronak Rathod
6,58020 silver badges46 bronze badges
-
We need to clean the cache?Masud Shaikh– Masud Shaikh2019年05月28日 12:11:21 +00:00Commented May 28, 2019 at 12:11
-
you need to setup:upgrade alsoRonak Rathod– Ronak Rathod2019年05月28日 12:12:16 +00:00Commented May 28, 2019 at 12:12
-
You can also define cache behavior in controller devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/…Dominic Pixie– Dominic Pixie2019年05月28日 12:14:31 +00:00Commented May 28, 2019 at 12:14
-
Ok, I will check and update you soon.Masud Shaikh– Masud Shaikh2019年05月28日 12:17:39 +00:00Commented May 28, 2019 at 12:17
-
ok nop happy codingRonak Rathod– Ronak Rathod2019年05月28日 12:18:09 +00:00Commented May 28, 2019 at 12:18
You don't send a response from your ajax controller.
result factory type should be json
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
Code Sample:
<?php namespace Magento\Contact\Controller\Index; use Magento\Framework\Controller\ResultFactory; class AjaxCall extends \Magento\Framework\App\Action\Action { public function __construct( \Magento\Framework\App\Action\Context $context ) { parent::__construct($context); } public function execute() { $this->_view->loadLayout(); $layout = $this->_view->getLayout(); $customBlock = $layout->createBlock( \Magento\Contact\Block\ContactForm::class, 'ajax.contact-us', ['data' => ['id' => 'test']] )->setTemplate('Magento_Contact::form.phtml'); $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); return $resultJson->setData( [ 'html' => $customBlock->toHtml(), 'message' => __('my message') ] ); } }
answered May 28, 2019 at 12:31
-
Source class "\Module\Ajaxcart\Controller\Cart\Result" for "Module\Ajaxcart\Controller\Cart\ResultFactory" generation does not exist.Masud Shaikh– Masud Shaikh2019年05月28日 12:49:02 +00:00Commented May 28, 2019 at 12:49
-
Please share your code and some issue at depency injection2019年05月28日 12:50:13 +00:00Commented May 28, 2019 at 12:50
-
Can you share chat boxMasud Shaikh– Masud Shaikh2019年05月28日 12:51:16 +00:00Commented May 28, 2019 at 12:51
-
Post your full code controller at chat room chat.stackexchange.com/rooms/94208/…2019年05月28日 13:01:59 +00:00Commented May 28, 2019 at 13:01
default
execute()function