使用JsonView的TYPO3 extbase控制器中的JSON编码异常
对于基于extbase的TYPO3 CMS扩展,我创建了一个ApiController,使用JsonView作为视图对象。返回值就像一个符咒,设置了正确的标题使用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
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
是重要的一行。