My Controller File
public function myAction() {
$response = array();
$response['myresponse'] = $this->myAjax();
$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
return $this->getResponse()->setBody(json_encode($response));
}
protected function myAjax()
{
$layout = $this->getLayout();
$totalsBlock = $layout->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml');
return $totalsBlock->toHtml();
}
My JSON Response
{"myresponse":" <table id=\"shopping-cart-totals-table\">\n <col \/>\n <col width=\"1\" \/>\n <tfoot>\n <tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n <strong>Grand Total<\/strong>\n <\/td>\n <td style=\"\" class=\"a-right\">\n <strong><span class=\"price\">$90.56<\/span><\/strong>\n <\/td>\n<\/tr>\n <\/tfoot>\n <tbody>\n <tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n Subtotal <\/td>\n <td style=\"\" class=\"a-right\">\n <span class=\"price\">$820.56<\/span> <\/td>\n<\/tr>\n<tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n Shipping & Handling (Flat Rate - Fixed) <\/td>\n <td style=\"\" class=\"a-right\">\n <span class=\"price\">$90.00<\/span> <\/td>\n<\/tr>\n<tr>\n <\/tr>\n <\/tbody>\n <\/table>\n"}
My Oncomplete function
new Ajax.Request("<?php echo $formAction;?>", {
method: 'post',
postBody: "mypostdata="+$('my_value').value,
onComplete: function(data) {
var mydata = data.responseText.evalJSON(true);
$('shopping-cart-totals-table').update(mydata);
}
});
The cart totals block is being updated but with incorrect HTML format. How could I make it in formatted data and show proper HTML in cart totals block ?
Please Help.
Thanks.
Best Answer
Instead of using
Try using
You are sending JSON with the key myresponse and your cart block html as the value for that key. So mydata will be an Object after json is converted into a JS object and this object should have a property myresponse with the HTML for the cart block.