Magento Ajax - 如何只获得身体部位?

时间:2010-01-19 00:16:12

标签: ajax magento

我正在尝试使用Magento进行ajax调用。当我通过Ajax调用阻止页面时,我获得了包括head,css,javascript和body在内的所有HTML。我如何才能获得身体部位?

1 个答案:

答案 0 :(得分:32)

如果您可以提供有关您正在调用的“阻止页面”的更多信息,则可能更容易识别该问题。默认情况下,Magento包含所有页面的<default>布局标记,即使在AJAX调用时也会为您提供页眉和页脚。

要发送一个没有额外的页面,你有几个选择。首先,您可以自己手动设置输出,完全避免布局系统。 Magento为单页结帐功能执行此操作:

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

您还可以修改此方法以使用这样的自定义布局处理程序:

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

在你的布局文件中:

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

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

如果要使用布局,第二个选项是定义备用默认布局。当您在Magento控制器中调用$this->loadLayout();时,您实际上可以指定除<default>以外的句柄。 Magento产品控制器的一个例子是:

$this->loadLayout('popup');

此布局默认在main.xml布局文件中定义,并呈现popup.phtml模板,可能适合您使用。

如果您仍有问题,请告诉我,我们可以尝试其他事情。希望有所帮助。

谢谢, 乔