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.