I need to create multiple ajax function in same controller , here is my controller
<?php
namespace testmoduleControllerResult;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;
use MagentoFrameworkControllerResultJsonFactory;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkHTTPClientCurl;
class Result extends MagentoFrameworkAppActionAction
{
/**
* @var MagentoFrameworkViewResultPageFactory
*/
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('RgfinmobOrangeBlockIndex')
// ->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('RgfinmobOrangeBlockIndex')
->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('RgfinmobOrangeBlockIndex')
// ->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('RgfinmobOrangeBlockIndexmodel')
->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