In my js file i have this ajax request :

var url = $(this).closest("form").attr('action');
                    type: "POST",
                    url: url,
                     data: {
                    "key": "test1",
                    "key2": "test2"
                dataType: 'json'
                }).done(function (msg) {

In my controller i have this code:

 public function execute()

         $response = [
            'errors' => false,
            'message' => 'Re-calculate successful.'
        $returnValue = '2';
        $result = $this->resultJsonFactory->create();
        $escaper = $this->_objectManager->get('Magento\Framework\Escaper');

        $id= $this->getRequest()->getPost('key2');

        if ($this->getRequest()->isAjax()) {
            $returnValue = 'OK';


        return $result->setData($returnValue);

When i make the ajax request i receive 2 which came from here $returnValue = '2';

But if i tired to get the $id= $this->getRequest()->getPost('key2'); i receive null.

so my problem is here:

if ($this->getRequest()->isAjax()) {
   $returnValue = 'OK';     

My if is not accessed.

Does anyone know what i am doing wrong?

Your Answer

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

Browse other questions tagged or ask your own question.