I tried to make a simple ajax-call to my controller.
Somehow I don't get a valid response.
MyCompany/MyModule/Index/GetAjax.php
namespace MyCompany\MyModule\Controller\Index;
class GetAjax extends \Magento\Backend\App\AbstractAction {
public function __construct(
Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
) {
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context);
}
public function execute() {
/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->resultJsonFactory->create();
return $result->setData(['success' => true]);
}
}
MyCompany/MyModule/etc/frontend/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="standard">
<route id="mymodule" frontName="mymodule">
<module name="MyCompany_MyModule" />
</route>
</router>
</config>
MyCompany/MyModule/view/frontend/web/js/test.js
define(['jquery'],
function ($) {
$(document).ready(function() {
$.ajax({
url : 'mymodule/index/getAjax',
type : 'GET',
data: {
format: 'json'
},
dataType:'json',
success : function(data) {
alert('Data: '+data);
},
error : function(request,error)
{
alert("Error");
}
});
});
});
I get my error message and in my console I get
JQMIGRATE: jQuery.parseJSON requires a valid JSON string.
Besides if I try to enter mymodule/index/getAjax
url in my browser there is a
Error 500 (internal server error)
Can anyone help me out?
\Magento\Framework\App\Action\Action
.