0

After making the ajax call, i try to recover the values, but returns null

this is function inside controller

    public function checkAction()
{
    # Retrieve value
    //$checkbox = Mage::app()->getRequest()->getPost('checkbox');
    //$checkbox = Mage::app()->getRequest()->getParam("checkbox");
    //$checkbox = $this->getRequest()->getParam("checkbox");
    $checkbox = $this->getRequest()->getPost("checkbox");

    Mage::log($checkbox);

    //response
    $response = array('namecheck' => $checkbox);

    # Send response Json
    $this->getResponse()->setHeader('Content-type', 'application/json');
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}

this is ajax call

    new Ajax.Request('<?php echo $ajaxUrl; ?>', {
    method: 'POST',
    data: {
        ajax : true,
        checkbox : id_var,
        value : value_var,
        },
    onSuccess: function(transport) {
        alert('Sent notification.');
    },
    onFailure: function(transport) {
        alert("Couldn't send a notification. ");
    }
});

tips ? I tried both getPost & getParams and method: 'POST' & method: 'GET'

Log: null

Payload response: {"namecheck":null}

4
  • Ajax call is working fine? – Sukumar Gorai Jul 24 '18 at 11:27
  • yes, it work fine, appear alert with sent notification, and payload appear {"namecheck":null} – Orazio D'Arrigo Jul 24 '18 at 11:30
  • Are you getting values on id_var? – Sukumar Gorai Jul 24 '18 at 11:31
  • yes I tried to replace with a string js = checkbox: 'hello', same result null – Orazio D'Arrigo Jul 24 '18 at 11:33
1

I solved this way, change data with params

var params = {id_check:id_checked, value_check:value_checked};
new Ajax.Request('<?php echo $ajaxUrl; ?>', {
    method: 'POST',
    cache : false,
    evalScripts: true,
    parameters:  params,
    onSuccess: function(transport) {
        alert('send');
    },
    onFailure: function(transport) {
        alert("Couldn't send a notification. ");
    }
});

and inside controller

    public function checkAction() {

    if($this->getRequest()->isXmlHttpRequest() && $this->getRequest()->isPost()){

        # Mage::log(print_r(Mage::app()->getRequest()->getParams(), true));

        $checkBoxId = Mage::app()->getRequest()->getParam('id_check');
        $checkValue = Mage::app()->getRequest()->getParam('value_check');

        # response
        $response = array( 'success' => 'success');

        # Send response Json
        $this->getResponse()->setHeader('Content-type', 'application/json');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
    } else {
        //other
    }
}
0

Try to use this below code :

Controller :

public function checkAction()
{
    # Retrieve value

    $checkbox = $this->getRequest()->getPost("checkbox");

    Mage::log($checkbox);

    //response
    $response = array('namecheck' => $checkbox);

    //Send response Json
    $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}

jQuery :

new Ajax.Request('<?php echo $ajaxUrl; ?>', {
    method: 'POST',
    data: {
        ajax : true,
        checkbox : 1,
        value : 1,
        },
    onSuccess: function(transport) {
        alert(transport.namecheck);
    }
});
5
  • nothing always value null, I think the problem is on the $ This-> getRequest () -> getPost ("checkbox"); I always come back to value null – Orazio D'Arrigo Jul 24 '18 at 11:40
  • use this : Mage::app()->getRequest()->getPost('checkbox'); instead of $this->getRequest () -> getPost ("checkbox"). – Rohan Hapani Jul 24 '18 at 11:42
  • if you see above I have already done the test with mage::app() – Orazio D'Arrigo Jul 24 '18 at 11:44
  • Yes. I already show this. Can you please upload your url path here? – Rohan Hapani Jul 24 '18 at 11:50

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.