Calling a controller with location
Vendor\Orderitemstatestatus\Controller\Adminhtml\orderitemstatestatusmapping\OrderStatus.php
The controller is called through AJAX from admin custom form as follows
setAfterElementHtml('
<script>
require(["jquery", "jquery/ui"], function($){
$(document).ready(function() {
$("#page_order_status").click(function() {
var customurl = "orderitemstatestatus/orderitemstatestatusmapping/orderstatus";
$.ajax({
url: customurl,
type: "POST",
showLoader: true,
data: {form_key: window.FORM_KEY},
success: function(response) {
console.log(response);
},
error: function (xhr, status, errorThrown) {
console.log(errorThrown);
}
});
});
});
});
</script>'
);
Is there something wrong with the url? The secret keys to url is disabled from the backend.
routes.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="admin">
<route id="orderitemstatestatus" frontName="orderitemstatestatus">
<module name="Vendor_Orderitemstatestatus" before="Magento_Adminhtml" />
</route>
</router>
</config>
OrderStatus.php file:
<?php
namespace Vendor\Orderitemstatestatus\Controller\Adminhtml\orderitemstatestatusmapping;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
class orderstatus extends \Magento\Backend\App\Action
{
/**
* @param Action\Context $context
*/
protected $resultJsonFactory;
public function __construct(Action\Context $context,\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory)
{
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context);
}
/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
// return Json(new {msg="Successfully added "});
return $this->resultJsonFactory->create()->setData(['data' => 'file successfully called']);
}
}
The controller should simply return "file successfully called in json format" but returns dashboard page html.