I am trying to get the response of my Ajax in the Controller :
require(['jquery', 'mage/url', 'mage/cookies'], function ($, url) {
'use strict';
window.updateStatus = function (element) {
var isEnabled = $(element).val();
var entityId = $(element).data('entity-id');
var formKey = $.mage.cookies.get('form_key'); // Get the form key
console.log('Form Key:', formKey);
console.log(url.build('customergroup/index/updatetoggle'));
// Log the form key to ensure it's being retrieved
$.ajax({
// url: url.build('customergroup/index/updatetoggle'),
url: url.build(window.BASE_URL + 'customergroup/index/updatetoggle'),
type: 'POST',
data: {
entity_id: entityId,
is_enabled: isEnabled,
form_key: formKey
},
success: function (response) {
if (response.error) {
console.error('Error:', response.message);
alert(response.message);
} else {
console.log('Status updated successfully', response);
}
},
error: function (xhr, status, error) {
console.error('Error updating status', error);
}
});
};
});
Controller File :
<?php
namespace VendorModuleControllerAdminhtmlIndex;
use MagentoBackendAppAction;
use MagentoFrameworkControllerResultJsonFactory;
use PsrLogLoggerInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkAppCsrfAwareActionInterface;
use MagentoFrameworkAppRequestInvalidRequestException;
class Updatetoggle extends Action
{
protected $resultJsonFactory;
protected $logger;
public function __construct(
ActionContext $context,
JsonFactory $resultJsonFactory,
LoggerInterface $logger
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->logger = $logger;
}
public function execute()
{
$result = $this->resultJsonFactory->create();
$responseData = ['success' => false];
try {
// Validate form key
if (!$this->_validateFormKey()) {
throw new LocalizedException(__('Invalid Form Key. Please refresh the page.'));
}
if (!$this->getRequest()->isAjax()) {
throw new LocalizedException(__('Invalid request.'));
}
$entityId = $this->getRequest()->getParam('entity_id');
$isEnabled = $this->getRequest()->getParam('is_enabled');
$this->logger->info("Toggle updated for Entity ID: $entityId, Enabled: $isEnabled");
$responseData['success'] = true;
$responseData['entity_id'] = $entityId;
$responseData['is_enabled'] = $isEnabled;
} catch (LocalizedException $e) {
$responseData['error'] = true;
$responseData['message'] = $e->getMessage();
$this->logger->error($e->getMessage());
} catch (Exception $e) {
$responseData['error'] = true;
$responseData['message'] = __('An error occurred while updating the status.');
$this->logger->error($e->getMessage());
}
return $result->setData($responseData);
}
}
Block File :
<?php
namespace VendorModuleBlockAdminhtmlCustomerEditTabViewRenderer;
use MagentoFrameworkDataObject;
use MagentoBackendBlockWidgetGridColumnRendererAbstractRenderer;
class EnableToggle extends AbstractRenderer
{
/**
* Render the HTML for the enable/disable toggle.
*
* @param DataObject $row
* @return string
*/
public function render(DataObject $row)
{
// Ensure the 'is_enabled' field exists in your data source
$isEnabled = (bool) $row->getData('is_enabled');
$entityId = $row->getId();
// Build the select element
$html = '<select class="admin__control-select" data-entity-id="' . $entityId . '" onchange="updateStatus(this)">';
$html .= '<option value="1"' . ($isEnabled ? ' selected' : '') . '>Enabled</option>';
$html .= '<option value="0"' . (!$isEnabled ? ' selected' : '') . '>Disabled</option>';
$html .= '</select>';
return $html;
}
}
routes.xml :
The response is in the ajax but not coming in the Controller.