gpt4 book ai didi

ajax - Magento Ajax - 如何只获得 body 部位?

转载 作者:行者123 更新时间:2023-12-03 15:19:58 26 4
gpt4 key购买 nike

我正在尝试将 ajax 调用与 Magento 一起使用。当我通过 Ajax 调用 block 页面时,我会得到所有的 HTML,包括 head、css、javascript 和 body。我如何只获得 body 部位?

最佳答案

如果您可以提供有关您正在调用哪个“阻止页面”的更多信息,则可能更容易识别问题。默认情况下,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>

如果要使用布局,第二个选项是定义替代的默认布局。当您调用 $this->loadLayout();在 Magento Controller 中,您实际上可以指定 <default> 以外的句柄从下降。 Magento 产品 Controller 的一个例子是:
$this->loadLayout('popup');

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

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

谢谢,

关于ajax - Magento Ajax - 如何只获得 body 部位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com