0

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.

  • what error you are facing? – aravind Jul 24 at 11:10
  • not going in execute() function even and when i open in browser then it works well. – sarvesh Dineshkumar Patel Jul 24 at 11:12
  • share your ajax code, also share screenshot of error – Mohit Rane Jul 24 at 11:19
  • please check i update my code – sarvesh Dineshkumar Patel Jul 24 at 11:40
  • Have you got the solution? – aravind Jul 24 at 11:48
0

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();
  • i will share ajax and controller code ok – sarvesh Dineshkumar Patel Jul 24 at 11:20
  • var request_data = 'variable pass'; 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("GET", window.request_url, true); xhttp.setRequestHeader("Content-type", "application/json"); xhttp.send(request_data); – sarvesh Dineshkumar Patel Jul 24 at 11:24
  • namespace Lucent\SocialLogin\Controller\Login; use Magento\Framework\App\Action\Action; class Request extends \Magento\Framework\App\Action\Action { public function __construct( \Magento\Framework\App\Action\Context $context, ) { return parent::__construct($context); } public function execute() { $data = array(); $data['redirect'] = ''; – sarvesh Dineshkumar Patel Jul 24 at 11:24
  • Post your code like you did with controller , such that it will be easy to debug.. – aravind Jul 24 at 11:30
  • check updated exact my code so you will get some ideas! – sarvesh Dineshkumar Patel Jul 24 at 11:37
0

I got a solution here. When I trying with $.post() method that time no need of form_key.

But, When I trying with var xhttp = new XMLHttpRequest(); with post method then form_key mandatory so that is why $_POST data not getting so quit from it.

Thanks all whoever supports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.