Magento – How to Post data from ajax get by post not working in postman

magento2.3.1post-data

Below is my code when I try to post data but not working

URL is

http://localhost/test1/modulename/login/request

my ajax code for javascript

var request_data = 'id='+id+'&name='+name+'&email='+email+'&social_type='+social_type;

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", window.request_url, true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(request_data);

Below is my controller code,

<?php
namespace Lucent\SocialLogin\Controller\Login;

use Magento\Framework\App\RequestInterface;

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
)
{
    parent::__construct($context);
    $this->data = $_REQUEST;
    print_r($this->data);
    $this->_customer = $customer;
    $this->_customerSession = $customerSession;
}


public function execute()
{
    print_r($this->data);
    echo '1';
    $post = $this->getRequest()->getPostValue();
    print_r($post);
    die();

}

}

So please help me. thanks

I updated by exact controller code so hope not it will help you to solve it.

Best Answer

Please die in construct and print all the parameters, and check you are posting form key or not.

Without it, you cannot get data to execute a function. And use the following method for getting parameters.

$params = $this->getReqest()->getPost();
Related Topic