I've set up the JS, and controller and checkout.html
I need to call the controller with ajax to getPrice() method.
My controller name is GetAjax.php and here is the controller code.
<?php
namespace Iostpay\Iostpaymagento\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class GetAjax extends \Magento\Framework\App\Action\Action
{
public function __construct(
Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
) {
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context);
}
public function execute()
{
print($this->getRequest()->getParam('customdata1'));
print("heuss");
die();
/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->resultJsonFactory->create();
return $result->setData(['success' => true]);
}
}
And the JS code which triggers on click of place order.
require(["jquery"], function ($) {
function transfer() {
var customurl = "modulename/index/GetAjax'";
$.ajax({
url: customurl,
type: 'POST',
dataType: 'json',
data: {
customdata1: 'test1',
customdata2: 'test2',
},
complete: function(response) {
console.log( response );
},
error: function (xhr, status, errorThrown) {
console.log('Error happens. Try again.');
}
});
}
});
The Error I get in console is
POST http://127.0.0.1/magento3/newmodule/GetAjax/execute 404 (Not Found)
How can I execute any specific method from the controller.