0

I'm creating an ajax add-to-cart module. It's working fine (adding product, respecting quantity, etc) BUT the response is always the full HTML page, instead of a JSON object. In particular, the responseJSON object is always NULL.

I'm overriding the core controller with my own:

File: app/code/local/My/Module/controllers/CartController.php

class My_Module_CartController extends Mage_Checkout_CartController
{
    public function addAction() {
        $params = $this->getRequest()->getParams();

        if ( !isset($params['my_module'] ) {
            return parent::addAction();
            exit();
        }

        try {
            ...
                // controller logic
            ...
        } catch (Exception $e) {
            ...
        }

        $jsonResponse = Mage::helper('core')
            ->jsonEncode(
                array(
                    'success' => $success,
                    'message' => $responseMsg
                )
            );

        $this->getResponse()->setHeader('Content-type', 'application/json');
        $this->getResponse()->setBody($jsonResponse);
    }
}

I'm then receiving this with my prototype (barf) function.

File: app/design/frontend/default/default/template/my_module/add-to-cart.phtml

<script type="text/javascript">
    //<![CDATA[
        productAddToCartForm.submit = function(button) {
            if (this.validator.validate()) {
                var form = this.form;
                try {
                    form.request({
                        parameters: { my_module: 'true', isAjax: '1' },
                        onSuccess: function(transport) {
                            console.log(transport); // <-- responseJSON is always NULL inside this object
                            ...
                        },
                        onFailure: function(transport) {
                            ...
                        }
                    });
                } catch (e) {}

                if (e) {
                    throw e;
                }
            }
        }.bind(productAddToCartForm);
    //]]>
</script>

I'm logging the full response from the server. The responseText object contains the entire HTML page, as it would render in a browser, while the responseJSON object is NULL.

So, my question is: how can I get a JSON response from the cart controller's addAction method? Thanks.

~ edit ~

FYI: There are no errors in the logs, either for Mage or the server.

  • 1
    The key is probably in your try catch, could you also provide that code. Either that or you can try putting mage logs to see where in the controller you actually get to. – David Manners Mar 31 '14 at 13:39
1

Try to edit your layout.xml for the ajax page:

<your_ajax_action>
        <reference name="root">
            <block type="yourajax/block" name="root" output="toHtml" />
        </reference>
</your_ajax_action>

This way, you will force the skipping of the headers, footers and other pages you don't need.

Alternatively you can try to simply echo your response and then exit() after. It is not the Magento way, but it usually works.

0

Oh.The.Pain. I found another module that was also extending the Mage Checkout CartController, but for no use at all; just commented code. Removed that and it's working perfectly now.

Thanks to all for looking.

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.