Get form_key
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$key_form = $objectManager->get('Magento\Framework\Data\Form\FormKey');
$form_Key = $key_form->getFormKey();
?>
Javascript code
<a href="javascript:;" onclick="Test()">Javascript</a>
<script type="text/javascript">
function Test(){
var request_data = 'id=125&form_key=<?php echo $form_Key; ?>';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
var url = data.redirect;
if(url!=''){
window.location.href = data.redirect;
}
}
};
xhttp.open("POST", "your_path", true);
//xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(request_data);
}
</script>
ccontroller code
<?php
namespace Vendor\ModuleName\Controller\Login;
class Test extends \Magento\Framework\App\Action\Action
{
protected $_customer;
protected $_customerSession;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Customer $customer,
\Magento\Customer\Model\Session $customerSession
)
{
$this->data = $_REQUEST;
print_r($this->data);
$this->_customer = $customer;
$this->_customerSession = $customerSession;
return parent::__construct($context);
}
public function execute()
{
print_r($_REQUEST);
echo '1';
die();
}
}
?>
Please check it will help you. Thanks.