In admin I have the following controller:
<?php
namespace Vendor\Module\Controller\Adminhtml;
use Magento\Backend\App\Action\Context;
class Test extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Backend\App\Action\Context $context
*/
public function __construct(
Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
}
public function execute()
{
return $this->resultJsonFactory->create()->setData([
'this' => 'test'
]);
}
}
in Vendor/Module/etc/adminhtml/routes.xml I have:
<?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="module" frontName="module">
<module name="Vendor_Module" />
</route>
</router>
</config>
In javascript I do the following ajax request to that controller:
jQuery.ajax( {
url: location.protocol + '//' + location.host+'/admin/module/test',
data: {form_key: window.FORM_KEY},
type: 'POST'
}).done(function(a) {
console.log(a);
});
And I receive 404 error
http://mydomain/admin/module/test?isAjax=true 404 (Forbidden)
as well.. same thing…
Why is this happening ?
Best Answer
you should make the call to
location.protocol + '//' + location.host+'/admin/vendor/module/test'
[EDIT]
or try
location.protocol + '//' + location.host+'/admin/module/test'