0

I need to create multiple ajax function in same controller , here is my controller

<?php
namespace test\module\Controller\Result;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\Client\Curl;
class Result extends \Magento\Framework\App\Action\Action
{
 /**
 * @var Magento\Framework\View\Result\PageFactory
 */
 protected $resultPageFactory;
 protected $curl;
 protected $resultJsonFactory; 
 /**
 * @param Context $context
 * @param PageFactory $resultPageFactory
 */
 public function __construct(
 Context $context,
 Curl $curl,
 PageFactory $resultPageFactory,
 JsonFactory $resultJsonFactory
 )
 {
 $this->curl = $curl;
 $this->resultPageFactory = $resultPageFactory;
 $this->resultJsonFactory = $resultJsonFactory; 
 return parent::__construct($context);
 }
 
 public function execute()
 {
 
 $numone = $this->getRequest()->getParam('numone');
 $numtwo = $this->getRequest()->getParam('numtwo');
 $result = $this->resultJsonFactory->create();
 $resultPage = $this->resultPageFactory->create();
 // $block = $resultPage->getLayout()
 // ->createBlock('Rgfinmob\Orange\Block\Index')
 // ->setTemplate('Rgfinmob_Orange::result.phtml')
 // ->setData('numone',$numone)
 // ->setData('numtwo',$numtwo)
 // ->toHtml();
 
 $URL = 'apiurl';
 
 
 //set curl options
 // $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
 $this->curl->setOption(CURLOPT_HEADER, 0);
 $this->curl->setOption(CURLOPT_TIMEOUT, 60);
 $this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
 $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
 //set curl header
 $this->curl->addHeader("Content-Type", "application/json");
 //get request with url
 $this->curl->get($URL);
 //read response
 $response = $this->curl->getBody();
 $resp= json_decode($response, TRUE);
 $block = $resultPage->getLayout()
 ->createBlock('Rgfinmob\Orange\Block\Index')
 ->setTemplate('Rgfinmob_Orange::result.phtml')
 ->setData('numone',$resp['make_list'])
 ->toHtml();
 $result->setData(['output' => $block]);
 // return $response;
 // $result->setData(['output' => $block]);
 return $result;
 }
 
 public function getVarient()
 {
 
 $brand = $this->getRequest()->getParam('brand');
 //$numtwo = $this->getRequest()->getParam('numtwo');
 $result1 = $this->resultJsonFactory->create();
 $resultPage1 = $this->resultPageFactory->create();
 // $block = $resultPage->getLayout()
 // ->createBlock('Rgfinmob\Orange\Block\Index')
 // ->setTemplate('Rgfinmob_Orange::result.phtml')
 // ->setData('numone',$numone)
 // ->setData('numtwo',$numtwo)
 // ->toHtml();
 
 $URL = 'apiurl';
 
 
 //set curl options
 // $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
 $this->curl->setOption(CURLOPT_HEADER, 0);
 $this->curl->setOption(CURLOPT_TIMEOUT, 60);
 $this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
 $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
 //set curl header
 $this->curl->addHeader("Content-Type", "application/json");
 //get request with url
 $this->curl->get($URL);
 //read response
 $response = $this->curl->getBody();
 $resp= json_decode($response, TRUE);
 $block = $resultPage1->getLayout()
 ->createBlock('Rgfinmob\Orange\Block\Index\model')
 ->setTemplate('Rgfinmob_Orange::result1.phtml')
 ->setData('model',$resp['model_list'])
 ->toHtml();
 $result1->setData(['output' => $block]);
 // return $response;
 // $result->setData(['output' => $block]);
 return $result;
 } 
}

here is my part of script

<script>
require(['jquery'],function(){
 jQuery(document).ready(function() {
 jQuery(".result").hide();
 
 
 var url = "<?php echo $block->getBaseUrl().'test/result/result/' ?>";
 jQuery.ajax({
 url: url,
 type: "POST",
 
 showLoader: true,
 cache: false,
 success: function(response){
 
 // console.log(response['success']['make_list']);
 jQuery(".result").show();
 jQuery(".result").html(response.output);
 }
 });
 return false;
 });
 jQuery(document).on('change', '.select_brand', function() {
 
 
 var val=jQuery(this).val();
 var url = "<?php echo $block->getBaseUrl().'test/result/result/getVarient/' ?>";
 jQuery.ajax({
 url: url,
 type: "POST",
 data: {val:val},
 showLoader: true,
 cache: false,
 success: function(response){
 
 // console.log(response['success']['make_list']);
 jQuery(".result1").show();
 jQuery(".result1").html(response.output);
 }
 });
 return false;
});
 
});
</script>

But both script calling same function , first script should call excecute and second method should call getvarient . how to achieve this

asked Jan 19, 2022 at 15:59

1 Answer 1

1

Magento 2 Controller has only one entry point by URL, so only execute method is called when you run it by URL.

You can call a different function from the execute method based on some condition or parameters. Following is the example.

public function execute()
{
 if(parameter1){
 $this->callFunctionOne();
 } elese {
 $this->callFunctionTwo();
 }
}

I hope it will help you.

answered Jan 19, 2022 at 16:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.