2

I have this code for a AJAX call:

Controller/Data/Get.php

<?php
    namespace Vendor\Module\Controller\Data;

    class Get extends \Magento\Framework\App\Action\Action
    {
        protected $resultJsonFactory;
        protected $resultFactory;

        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
            \Magento\Framework\Controller\ResultFactory $resultFactory
        )
        {
            $this->resultJsonFactory = $resultJsonFactory;
            $this->resultFactory = $resultFactory;

            parent::__construct($context);
        }

        public function execute()
        {
            $result = $this->resultJsonFactory->create();
            $response = '200';

            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($response);

            return $resultJson;
        }
    }

With my js in my .phtml template file as:

<script type="text/javascript">
    require(['jquery', 'module'], function($, script)
    {
        //<![CDATA[
        jQuery(document).ready(function($)
        {
            $('select.filter-select').change(function()
            {
                let ajaxReq,
                    value = $(this).val();

                ajaxReq = $.ajax({
                    url: '<?php echo $block->getUrl("module/data/get"); ?>',
                    type: 'post',
                    showLoader: true,
                    data: {val: value}
                });

                ajaxReq.done(function(res, txtStatus, jqXHR)
                {
                    console.log(res);
                });

                ajaxReq.fail(function(XMLHttpRequest, txtStatus, err)
                {
                    console.log(XMLHttpRequest);
                    console.log(txtStatus);
                    console.log(err);
                });
            });
        });
        //]]>
    });
</script>

This returns:

There has been an error processing your request

Exception printing is disabled by default for security reasons.

Error log record number: 616547725800

Going to var/report/616547725800 shows:

{"0":"Source class \"\Vendor\Module\Controller\Data\Result\" for \"Vendor\Module\Controller\Data\ResultFactory\" generation does not exist."

This happens both before and after the below commands:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush

What's wrong with my code? :(

3

Change $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); this line.

Use following line:


$resultJson = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.