I've created a controller within my custom module to add items to my basket and i'm trying to make an ajax call across to it. However, I can't even call my controller currently as I get a 404 error from my ajax call.
This is how my Controller currently looks:
<?php
namespace Vendor\TestModule\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Test extends Action
{
public function __construct(
Context $context
) {
parent::__construct($context);
}
public function execute()
{
return "Success";
}
}
This is the ajax call in my phtml file:
var data = "";
var addToCartUrl = "<?php echo $block->getUrl('testmodule/index/test'); ?>";
$.ajax({
url : addToCartUrl,
dataType : 'json',
type : 'POST',
data: data,
success : function(res)
{
console.log('Success');
},
error : function()
{
console.log('error');
}
});
Here is my routes.xml file:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="test_testmodule" frontName="test_testmodule">
<module name="Test_TestModule" />
</route>
</router>
I've tried to manually visit the URL that i'm sending the ajax call to and that doesn't display anything either.
test_testmodule/index/test
in call url