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

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

        $jsonResponse = Mage::helper('core')
                    'success' => $success,
                    'message' => $responseMsg

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

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">
        productAddToCartForm.submit = function(button) {
            if (this.validator.validate()) {
                var form = this.form;
                try {
                        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;

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. Mar 31, 2014 at 13:39

2 Answers 2


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

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

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.


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.