JSON 使用 JsonView 在 TYPO3 extbase 控制器中编码异常
JSON encoded exceptions in TYPO3 extbase controller with JsonView
对于我的 extbase-based TYPO3 CMS 扩展,我创建了一个带有 JsonView 作为视图的 ApiController object。返回值就像一个魅力,设置了正确的 header Content-type: application/json
。
对于 return 其他响应,例如缺少授权消息或验证错误,我目前使用:
$data = ["errors" => [
"status" => 401,
"message" => "Missing access token"
]];
$this->throwStatus($status, null, json_encode($data));
当我使用 $this->throwStatus()
时,设置了 header Content-type: text/html
。即使我在使用 $this->throwStatus()
.
之前手动设置 header("Content-type: application/json");
如何创建具有正确内容类型的回复header?
在抛出状态之前,尝试在响应中设置 headers object:
$this->response->setHeader('Content-Type', 'application/json', true);
$this->response->sendHeaders();
如果您通过专用 pageType 访问数据,您可以在 TypoScript 中为此 pageType 设置 header:
myPageType.config.additionalHeaders {
10 {
header = Content-Type: application/json
replace = 1
}
}
我会将这个添加到我的 post 关于主题:https://usetypo3.com/json-view.html
对于我的 extbase-based TYPO3 CMS 扩展,我创建了一个带有 JsonView 作为视图的 ApiController object。返回值就像一个魅力,设置了正确的 header Content-type: application/json
。
对于 return 其他响应,例如缺少授权消息或验证错误,我目前使用:
$data = ["errors" => [
"status" => 401,
"message" => "Missing access token"
]];
$this->throwStatus($status, null, json_encode($data));
当我使用 $this->throwStatus()
时,设置了 header Content-type: text/html
。即使我在使用 $this->throwStatus()
.
header("Content-type: application/json");
如何创建具有正确内容类型的回复header?
在抛出状态之前,尝试在响应中设置 headers object:
$this->response->setHeader('Content-Type', 'application/json', true);
$this->response->sendHeaders();
如果您通过专用 pageType 访问数据,您可以在 TypoScript 中为此 pageType 设置 header:
myPageType.config.additionalHeaders {
10 {
header = Content-Type: application/json
replace = 1
}
}
我会将这个添加到我的 post 关于主题:https://usetypo3.com/json-view.html