json - 服务器返回 text/html header 而不是 application/json header

标签 json ubuntu zend-framework2 restful-architecture

我的 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/

相关文章:

javascript - AngularJs : json parsing

jQuery数据表json初始化无效json

ubuntu - Windows 10 中 Ubuntu 中的 tkinter。错误 : "no display name and no $DISPLAY environment variable"

php - Zend 框架 2 'quote_identifiers' => false

jquery - 从 json "data"获取 jquery 中集合的计数

java - 如何以编程方式使用 JSON 文件中的数据填充 Firebase?

c - 如何在c中编写正则表达式来查找字符串是否为十六进制

windows - WSL 从 Windows 运行 linux 而无需生成 cmd-window

php - Doctrine 2 和 Zend Framework 2.2 的数据库名称注释

zend-framework2 - Zend\Form\Form 不显示来自自定义验证器的错误消息