Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JsonView的TYPO3 extbase控制器中的JSON编码异常_Typo3_Extbase_Typo3 7.6.x - Fatal编程技术网

使用JsonView的TYPO3 extbase控制器中的JSON编码异常

使用JsonView的TYPO3 extbase控制器中的JSON编码异常,typo3,extbase,typo3-7.6.x,Typo3,Extbase,Typo3 7.6.x,对于基于extbase的TYPO3 CMS扩展,我创建了一个ApiController,使用JsonView作为视图对象。返回值就像一个符咒,设置了正确的标题Content-type:application/json 要返回其他响应,如缺少授权消息或验证错误,我当前使用: $data = ["errors" => [ "status" => 401, "message" => "Missing access token" ]]; $this->throwS

对于基于extbase的TYPO3 CMS扩展,我创建了一个ApiController,使用JsonView作为视图对象。返回值就像一个符咒,设置了正确的标题
Content-type:application/json

要返回其他响应,如缺少授权消息或验证错误,我当前使用:

$data = ["errors" => [
    "status" => 401,
    "message" => "Missing access token"
]];
$this->throwStatus($status, null, json_encode($data));
当我使用
$this->throwStatus()
时,设置了标题
内容类型:text/html
。即使我手动设置
标题(“内容类型:application/json”)$this->throwStatus()
之前,先执行code>


如何使用正确的内容类型标题创建响应?

在抛出状态之前,请尝试在响应对象中设置标题:

$this->response->setHeader('Content-Type', 'application/json', true);
$this->response->sendHeaders();
如果您通过专用页面类型访问数据,则可以在打字脚本中设置此页面类型的标题:

myPageType.config.additionalHeaders {
   10 {
      header = Content-Type: application/json
      replace = 1
   }
}

我将在我的帖子中添加以下主题:

您是否尝试使用
$this->response->setHeader('Content-type','application/json',true)设置标题
replace=1
是重要的一行。