Best way to output ajax data from a Magento Admin Extension

隐身守侯 提交于 2019-11-29 10:57:41
Alan Storm

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;

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

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.

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!

$this->getResponse()->setBody($output)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!