我的 RESTful 服务是用 ZF2 编写的。它在我的本地 Windows wamp 中正常工作。它在 Ubuntu 服务器中返回 text/html 而不是 application/json header 。任何想法?
最佳答案
使用 application/json
返回 JSON 格式输出的最佳方式来自 Controller 的 header 启用 JsonStrategy 在应用程序配置的 view_manager
部分配置。
为此,请打开 module/Application/config/module.config.php
文件和:
return array(
...
'view_manager' => array(
//...
'strategies' => array(
'ViewJsonStrategy',
),
//...
),
...
);
在要返回 JSON 响应的 Controller 中:
<?php
namespace Application\Controller;
use Zend\View\Model\JsonModel; // Notice this line
class YourController extends AbstractActionController
{
public function exampleAction() {
$data = array('foo' => 'bar');
return new JsonModel($data);
}
}
希望能帮助到你!
关于json - 服务器返回 text/html header 而不是 application/json header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947973/