I have some questions on an Ajax call in admin area.
This code returns an error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Vendor\Module\Controller\Adminhtml\Index\Customajax::__construct(), 1 passed
<?php namespace Vendor\Module\Controller\Adminhtml\Index; class Customajax extends \Magento\Backend\App\Action { protected $resultPageFactory; protected $resultJsonFactory; public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory ) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; $this->resultJsonFactory = $resultJsonFactory; } public function execute() { if($this->getRequest()->isAjax()){ return $this->resultJsonFactory->create()->setData(['Test-Message' => 'hi']); } return $this->resultJsonFactory->create(); } }
Also, I would like to know how to get base url :
define([ 'jquery', 'underscore', 'uiRegistry', 'Magento_Ui/js/form/element/select', 'Magento_Ui/js/modal/modal', 'mage/url' ], function ($, _, uiRegistry, select, modal, url) { 'use strict'; return select.extend({ /** * On value change handler. * * @param {String} value */ onUpdate: function (value) { console.log('Selected Value: ' + value); var field1 = uiRegistry.get('index = field1'); var field2 = uiRegistry.get('index = field2'); field2.hide(); var field3Depend1 = uiRegistry.get('index = field3Depend1'); var linkUrl = url.build('index/customajax'); $.ajax( url: '../../'+linkUrl, showLoader: true, data: {form_key: window.FORM_KEY, 'value':value}, type: "POST", dataType : 'json', success: function(result){ alert(result); } }); return this._super(); }, }); });
Solved! Go to Solution.
Hello @tvgarden,
Please run below command in Magento root directory and try it
rm -rf generated/ php bin/magento setup:static-content:deploy -f
When you get this type of error Uncaught ArgumentCountError: Too few arguments, Please remove generate folder then try it.
--
If my answer is useful, please Accept as Solution & give Kudos
Hello @tvgarden
Remove your generated folder and then check your work.
Moreover, you have added the duplicate class in your __construct() function.
You have added the same class for $resultPageFactory and $resultJsonFactory.
Fixed the class name as given below
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
And for getting the URL in your ajax call, do as given below.
var linkUrl = url.build('frontname/index/customajax');
$.ajax( url: linkUrl, ...
... });
You will get the frontname of your module in app/code/Vendor/Module/etc/adminhtml/routes.xml
Hello @tvgarden,
Please run below command in Magento root directory and try it
rm -rf generated/ php bin/magento setup:static-content:deploy -f
When you get this type of error Uncaught ArgumentCountError: Too few arguments, Please remove generate folder then try it.
--
If my answer is useful, please Accept as Solution & give Kudos
Thank you for the replies.
I did both solutions and it finally worked.
@gelanivishal wrote:Hello @tvgarden,
Please run below command in Magento root directory and try it
rm -rf generated/ php bin/magento setup:static-content:deploy -fWhen you get this type of error Uncaught ArgumentCountError: Too few arguments, Please remove generate folder then try it.
--
If my answer is useful, please Accept as Solution & give Kudos