Magento – Retrieve Values passed in Ajax Magento 1.9

admin-controlleradminhtmlajax

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}

Best Answer

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
    }
}
Related Topic