I am trying to access a URL via AJAX in Magento 2's admin area. I have tried many things but every time the response is 404 Forbidden.
This is the code my module has:
Vendor/Module/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
</module>
</config>
Vendor/Module/etc/adminhtml/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="adminhtml">
<module name="Vendor_Module" before="Magento_Backend" />
</route>
</router>
</config>
AJAX JS Code using Prototype:
new Ajax.Request('<?php /* @escapeNotVerified */ echo $block->getAdminUrl(); ?>adminhtml/action/add', {
method: 'post',
parameters: {'order_id' : <?php /* @escapeNotVerified */ echo $block->getOrderId(); ?>},
onSuccess: function(response) {
console.log($response);
this.add();
}.bind(this)
});
Vendor/Module/Controller/Adminhtml/Action/Add.php
<?php
namespace Vendor\Module\Controller\Adminhtml\Action;
class Add extends \Magento\Backend\App\Action
{
protected $_context;
protected $_pageFactory;
protected $_jsonEncoder;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Json\EncoderInterface $encoder,
\Magento\Framework\View\Result\PageFactory $pageFactory
) {
$this->_context = $context;
$this->_pageFactory = $pageFactory;
$this->_jsonEncoder = $encoder;
parent::__construct($context);
}
public function execute()
{
$response = array('status' => 'success');
$this->getResponse()->representJson($this->_jsonEncoder->encode($response));
return;
}
}
Please tell me how I can access this admin URL via AJAX.



