1

In my custom module have a custom ajax request for sending selected date (datepicker) to controller and found this exception when access controller

Block1 exception(s): Exception #0 (BadMethodCallException): Missing required argument $resultJsonFactory of Larsa\ASlider\Controller\Index\indexquote

Controller:

namespace Larsa\ASlider\Controller\Index; 
class Index extends \Magento\Framework\App\Action\Action{
protected $resultJsonFactory;

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

public function execute()
{
    $result = $this->resultJsonFactory->create();
    if ($this->getRequest()->isAjax())
    {
        return $result->setData([
            'test'=>'test'
        ]);
    }
}}

JS:

var AjaxUrl = 'workshop/index/index'; 
    $('#datePicker').datepicker({
        onSelect: function (date) {
            $.ajax({
                showLoader: true, 
                url: AjaxUrl,
                data: {
                    date: $("#datePicker").datepicker("getDate")
                },
                type: "POST",
                dataType: 'json'
            }).done(function (data) {
                alert('success');
            }).error(function () {
                alert('error');
            });
        }
    });
    $('#datePicker').show(); 

Thanks in advance for help.

3
  • Please share the full code Action file?
    – Amit Bera
    Jan 29, 2019 at 14:34
  • Am just edited the post
    – Nasouh
    Jan 29, 2019 at 14:41
  • Is this frontend URL?
    – Amit Bera
    Jan 29, 2019 at 14:56

2 Answers 2

0

As this frontend URL

Then you cannot inject admin context class \Magento\Backend\App\Action\Context $context, here. That way you are getting the error.

Change this class to \Magento\Framework\App\Action\Context $context

after that do the Di compile properly.

7
  • 1
    wait, how do you know that it's a backend request? Does not seem like it. I would argue that the Interceptor class likely must be deleted / regenerated Jan 29, 2019 at 14:54
  • aah,you are right. We are not sure this is frontend or admin URL.
    – Amit Bera
    Jan 29, 2019 at 14:56
  • its frontend url
    – Nasouh
    Jan 29, 2019 at 14:58
  • what do you mean by "Di compile" ?
    – Nasouh
    Jan 29, 2019 at 15:05
  • setup:di: compile
    – Amit Bera
    Jan 29, 2019 at 15:14
0

Use \Magento\Framework\App\Action\Context $context

Instead of :

\Magento\Backend\App\Action\Context $context

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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