Question:

Can I make separate controller for AJAX actions? (Magento 1.x)

Kennedy: 5 days ago

I have my custom module and everything works fine, but I would like to create separately controller for AJAX actions (AjaxController). Currently I Have only IndexController. Everytime I get:

{"error":true,"message":"Invalid Form Key. Please refresh the page."}

Code snippets are below:

Build URL:

<script type="text/javascript">
    RECALC_SP_URL = "<?=$this->htmlEscape(Mage::helper('adminhtml')->getUrl('*/productcreator_ajax/getSpecialPrice/'));?>";
</script>

**Part of config.xml*:*

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <productcreator after="Mage_Adminhtml">Aiti_ProductCreator_Adminhtml</productcreator>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

AJAX call:

jQuery( "#margin_percent" ).change(function() {
    var $_this = jQuery( this );
    var store_id = 0;
    new Ajax.Request(RECALC_SP_URL, {
        method: 'post',
        parameters: {
            storeId: store_id,
            margin: $_this.val(),
            productId: jQuery('#productcreator_product_id').val(),
        },
        onSuccess: function (transport) {
            var response = JSON.parse(transport.responseText);

            jQuery("#special_price").val(response['special_price']);
        },
        onFailure: function () {

        }
    });
});

Thank you for some help!

Answer:
Ryleigh: 5 days ago

Below i have add new ajax request url in code. please review it

jQuery( "#margin_percent" ).change(function() {     
    var $_this = jQuery( this );
    var store_id = 0;
    /* margin is controller name. file name is like "marginController.php" & getPercent is function */
    new Ajax.Request('ajaxdata/margin/getPercent/', {
        method: 'get',
        parameters: {
            storeId: store_id,
            margin: $_this.val(),
            productId: jQuery('#productcreator_product_id').val(),
        },
        onSuccess: function (transport) {
            var response = JSON.parse(transport.responseText);

            jQuery("#special_price").val(response['special_price']);
        },
        onFailure: function () {

        }
    });
});