从Magento管理扩展输出ajax数据的最佳方法

从Magento管理扩展输出ajax数据的最佳方法,ajax,magento,magento-1.4,Ajax,Magento,Magento 1.4,我正在编写一个Magento管理扩展,其中包含一些ajax回调。到目前为止,我一直在使用控制器中的一个简单echo语句通过ajax调用反馈json。它“有效”,但我的日志文件中出现了一系列类似这样的错误: 2010-12-14T15:37:05+00:00调试(7):已发送的标题:[0]/home/simplifiedsafety/www/store/app/code/core/Mage/core/Controller/Response/Http.php:44 [0] /home/simpli

我正在编写一个Magento管理扩展,其中包含一些ajax回调。到目前为止,我一直在使用控制器中的一个简单echo语句通过ajax调用反馈json。它“有效”,但我的日志文件中出现了一系列类似这样的错误:

2010-12-14T15:37:05+00:00调试(7):已发送的标题:[0]/home/simplifiedsafety/www/store/app/code/core/Mage/core/Controller/Response/Http.php:44 [0] /home/simplifiedsafety/www/store/app/code/core/Mage/Core/Controller/Response/Http.php:44 [1] /home/simplifiedsafety/www/store/lib/Zend/Controller/Response/Abstract.php:727 [2] /home/simplifiedsafety/www/store/app/code/core/Mage/Core/Controller/Response/Http.php:75 [3] /home/simplifiedsafety/www/store/app/code/core/Mage/Core/Controller/Varien/Front.php:188 [4] /home/simplifiedsafety/www/store/app/code/core/Mage/Core/Model/App.php:304 [5] /home/simplifiedsafety/www/store/app/Mage.php:599 [6] /home/simplifiedsafety/www/store/index.php:104 [1] /home/simplifiedsafety/www/store/lib/Zend/Controller/Response/Abstract.php:727 [2] /home/simplifiedsafety/www/store/app/code/core/Mage/core/Controller/Response/Http.php:75 [3] /home/simplifiedsafety/www/store/app/code/core/Mage/core/Controller/Varien/Front.php:188 [4] /home/simplifiedsafety/www/store/app/code/core/Mage/core/Model/app.php:304 [5] /home/simplifiedsafety/www/store/app/Mage.php:599 [6] /home/simplifiedsafety/www/store/index.php:104

我想,为了避免这种情况,我需要通过某种方式将其推出。有人能给我一些指导吗

Magento用于将输出发送回浏览器。即使从控制器调用
renderLayout
,Magento也只是在内存中建立字符串输出,然后再输出。出现此错误的原因是控制器调度后有系统代码试图设置标头,但意外的控制器输出阻止设置这些标头

$this->getResponse()->setBody($output)
最简单的解决方法是抛出一个

exit;
在控制器输出后直接输入。这将停止执行,您的ajax响应将被发送,整个世界都很高兴。高兴

或者,如果您正在寻找一种始终难以捉摸的“正确”方式,根据核心中的示例,您可以从控制器调用以下命令来检索响应对象,然后直接设置其主体

$this->getResponse()->setBody('Some Response');
如果执行上述操作,您将绕过Magento布局系统,直接设置输出,但保留发送带有响应对象的输出的责任

您可能希望为头文件(JSON、XML等)设置自己的值,您可以使用以下操作(同样,通过控制器操作)

祝你好运

$this->getResponse()
->clearHeaders()
->setHeader('Content-Type', 'text/xml')
->setBody('Some Response');