php 如何从 Zend 只返回 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14777324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return only JSON from Zend
提问by Gia Duong Duc Minh
I'm using Zend Framework 1.x for my project. I want to create a Web service return only JSON string for the caller function. I tried to use Zend_Controller_Actionand applied those ways:
我的项目使用 Zend Framework 1.x。我想为调用者函数创建一个只返回 JSON 字符串的 Web 服务。我尝试使用Zend_Controller_Action和应用这些方式:
1.
1.
$this->getResponse()
->setHeader('Content-type', 'text/plain')
->setBody(json_encode($arrResult));
2.
2.
$this->_helper->getHelper('contextSwitch')
->addActionContext('nctpaymenthandler', 'json')
->initContext();
3.
3.
header('Content-type: application/json');
4.
4.
$this->_response->setHeader('Content-type', 'application/json');
5.
5.
echo Zend_Json::encode($arrResult);
exit;
6.
6.
return json_encode($arrResult);
7.
7.
$this->view->_response = $arrResult;
But when I used cURL to get result, it returned with JSON string surrounded by some HTML tags. Then I tried to user Zend_Rest_Controllerwith the options above. It still did not success.
但是当我使用 cURL 获取结果时,它返回带有一些 HTML 标记包围的 JSON 字符串。然后我尝试Zend_Rest_Controller使用上述选项。它仍然没有成功。
P.S.: Most of those ways above are from the question which had been asked on Stack Overflow.
PS:上面的大多数方法都来自 Stack Overflow 上提出的问题。
回答by Venu
I Like this way!
我喜欢这种方式!
//encode your data into JSON and send the response
$this->_helper->json($myArrayofData);
//nothing else will get executed after the line above
回答by Gerard Roche
You need to disable the layout and view rendering.
您需要禁用布局和视图渲染。
Explicit disable layout and view renderer:
显式禁用布局和视图渲染器:
public function getJsonResponseAction()
{
$this->getHelper('Layout')
->disableLayout();
$this->getHelper('ViewRenderer')
->setNoRender();
$this->getResponse()
->setHeader('Content-Type', 'application/json');
// should the content type should be UTF-8?
// $this->getResponse()
// ->setHeader('Content-Type', 'application/json; charset=UTF-8');
// ECHO JSON HERE
return;
}
If your using the json controller action helper you need to add a json context to the action. In this case the json helper will disable the layout and view renderer for you.
如果您使用 json 控制器操作助手,则需要向操作添加 json 上下文。在这种情况下,json 助手将为您禁用布局和视图渲染器。
public function init()
{
$this->_helper->contextSwitch()
->addActionContext('getJsonResponse', array('json'))
->initContext();
}
public function getJsonResponseAction()
{
$jsonData = ''; // your json response
return $this->_helper->json->sendJson($jsonData);
}
回答by Tim Fountain
Your code would need to disable the layout as well in order to stop the content being wrapped with the standard page template. But a much easier approach would just be:
您的代码还需要禁用布局以停止使用标准页面模板包装的内容。但更简单的方法是:
$this->getHelper('json')->sendJson($arrResult);
the JSON helper will encode your variable as JSON, set the appropriate headers and disable the layout and view script for you.
JSON 助手会将您的变量编码为 JSON,设置适当的标头并为您禁用布局和查看脚本。
回答by Alex
It is much easier.
这要容易得多。
public function init()
{
parent::init();
$this->_helper->contextSwitch()
->addActionContext('foo', 'json')
->initContext('json');
}
public function fooAction()
{
$this->view->foo = 'bar';
}

