1

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!

1 Answer 1

0

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 () {

        }
    });
});
6
  • My URL is ok, I'm sure - If ajax is sending by get method, all is ok....problem is only with post method.
    – Sekiphp
    Commented Jul 23, 2018 at 14:39
  • using GET method. I have edit code please review it.
    – Shorabh
    Commented Jul 24, 2018 at 4:15
  • Is is not possible to use post method?
    – Sekiphp
    Commented Jul 25, 2018 at 6:20
  • Yes . you to just edit method. Like method: 'post', or method: 'get',
    – Shorabh
    Commented Jul 25, 2018 at 6:21
  • If I edit method to post, I have problem with form key - {"error":true,"message":"Invalid Form Key. Please refresh the page."}
    – Sekiphp
    Commented Jul 25, 2018 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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