从Magento管理扩展的最佳方式输出AJAX数据 [英] Best way to output ajax data from a Magento Admin Extension

查看:102
本文介绍了从Magento管理扩展的最佳方式输出AJAX数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Magento管理扩展,它有一定的AJAX回调在里面。到现在为止,我一直回响着JSON我反馈通过与控制器的简单的回声声明Ajax调用。它作品,但我得到了一堆这样的错误在我的日志文件:

I'm writing a Magento Admin extension that has some ajax callbacks in it. Up until now I've been echoing the json I'm feeding back through the ajax call with a simple echo statement in the controller. It "works" but I get a bunch of errors like this in my log file:

2010-12-14T15:37:05 + 00:00 DEBUG(7):接头已经发出:

2010-12-14T15:37:05+00:00 DEBUG (7): HEADERS ALREADY SENT:

[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

我想避免这种情况我需要把它通过某种块。有人可以给我在这一点指导?

I think to avoid this I need to push it out through some sort of block. Can someone give me a little guidance on this?

推荐答案

Magento的使用一个响应对象来输出发送回浏览器。即使当你调用 renderLayout 从控制器,Magento的仅仅是建立在内存中的字符串输出输出之前。你得到这个错误的原因是有系统code控制器调度多数民众赞成在尝试设置头后,但你意想不到的控制器输出prevents这些头被设置。

Magento uses a response object to send output back to the browser. Even when you call renderLayout from a controller, Magento is just building up the string output in memory before outputting it. The reason you're getting this error is there's system code after the controller dispatch that's attempting to set headers, but your unexpected controller output prevents those headers from being set.

最简单的办法是抛出一个

The simplest solution is throw an

exit;

在您的直接控制器输出之后。这个暂停执行,你的Ajax响应被发送,这个世界是快乐的。麾。

in directly after your controller output. This halts execution, your ajax response is sent, the world is happy. Rejoice.

另外,如果你正在寻找总是难以捉摸的正确的方式,根据核心的例子,它看起来像你还可以从你的控制器以下检索响应对象,然后直接设置它的身体。

Alternately, if you're looking for that always elusive "right" way, based on examples in the core, it looks like you can call the following from your controller to retrieve the response object, and then set its body directly.

$this->getResponse()->setBody('Some Response');

如果你做到上面,你绕过Magento的布局系统和直接设置输出,但保持与发送响应对象输出的责任。

If you do the above, you're bypassing the Magento layout system and setting output directly, but keeping the responsibility of sending the output with the response object.

您可能要设置自己的值的报头(JSON,XML等),您可以像下面这样做(再次,从控制器动作)

You may want to set your own values for headers (JSON, XML, etc), which you can do with something like the following (again, from a controller action)

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

祝你好运!

Good luck!

这篇关于从Magento管理扩展的最佳方式输出AJAX数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆