Chr*_*ies 2 typo3 extbase typo3-7.6.x
对于我基于 extbase 的 TYPO3 CMS 扩展,我创建了一个 ApiController,以 JsonView 作为视图对象。返回值就像一个魅力,Content-type: application/json设置了正确的标题。
要返回其他响应,例如缺少授权消息或验证错误,我目前使用:
$data = ["errors" => [
"status" => 401,
"message" => "Missing access token"
]];
$this->throwStatus($status, null, json_encode($data));
Run Code Online (Sandbox Code Playgroud)
当我使用$this->throwStatus()标题时 Content-type: text/html设置。即使我header("Content-type: application/json");在使用$this->throwStatus().
如何使用正确的内容类型标头创建响应?
在抛出状态之前,尝试在响应对象中设置标头:
$this->response->setHeader('Content-Type', 'application/json', true);
$this->response->sendHeaders();
Run Code Online (Sandbox Code Playgroud)
如果您通过专用 pageType 访问数据,则可以在 TypoScript 中设置此 pageType 的标题:
myPageType.config.additionalHeaders {
10 {
header = Content-Type: application/json
replace = 1
}
}
Run Code Online (Sandbox Code Playgroud)
我会将其添加到我关于该主题的帖子中:https : //usetypo3.com/json-view.html