Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Ajax - How to get only body part?

Tags:

ajax

magento

I am trying to use ajax call with Magento. When I call a block page via Ajax, I get all the HTML including head, css, javascript, and body. How do I get only body part?

like image 737
Moon Avatar asked Jan 19 '10 00:01

Moon


1 Answers

If you can provide a little more information about which "block page" you are calling it may be easier to discern the issue. By default, Magento includes the <default> layout tag for all pages, which will give you the page headers and footers even on AJAX calls.

To send a page without all that extra, you have a few options. Firstly, you can simple set the output manually on your own, avoiding the layout system entirely. Magento does this for the one-page checkout feature:

$result = array( 'foo' => 'foo', 'bar' => 'bar', );
$this->getResponse()->setBody(Zend_Json::encode($result));

You can also modify this method to use a custom layout handler like this:

protected function loadPage() {
    $layout = $this->getLayout();
    $update = $layout->getUpdate();
    $update->load('your_custom_handle');
    $layout->generateXml();
    $layout->generateBlocks();
    $output = $layout->getOutput();

    $result = array( 'outputHtml' => $output, 'otherVar' => 'foo', );
    $this->getResponse()->setBody(Zend_Json::encode($result));        
}

And in your layout file:

<your_custom_handle>
    <remove name="right"/>
    <remove name="left"/>

    <block type="module/block" name="root" output="toHtml" template="module/template.phtml"/>
</your_custom_handle>

A second option, if you want to use layouts, is to define an alternative default layout. When you call $this->loadLayout(); in Magento controllers, you can actually specify a handle other than <default> to descend from. An example from the Magento product controller would be:

$this->loadLayout('popup');

This layout is defined by default within the main.xml layout file, and renders the popup.phtml template, and may be suitable for your use.

If you still have trouble, let me know and we can try other things. Hope that helps.

Thanks, Joe

like image 101
Joe Mastey Avatar answered Oct 08 '22 23:10

Joe Mastey