2017-02-27 29 views
2

對於我的基於extbase的TYPO3 CMS擴展,我創建了一個ApiController和JsonView作爲視圖對象。返回值就像一個魅力,設置正確的標題Content-type: application/jsonJSON編碼異常在TYPO3 extbase控制器與JsonView

要返回其他響應像缺少授權或驗證錯誤,我目前使用:

$data = ["errors" => [ 
    "status" => 401, 
    "message" => "Missing access token" 
]]; 
$this->throwStatus($status, null, json_encode($data)); 

當我使用$this->throwStatus()Content-type: text/html設置。即使我在使用$this->throwStatus()之前手動設置了header("Content-type: application/json");

如何使用正確的內容類型標題創建響應?

+0

你試過設置標頭'$這個 - >應答爲>的setHeader(「內容類型」,「應用lication/json',true);'? – undko

回答

2

之前你扔的狀態,嘗試設置頭的響應對象:

$this->response->setHeader('Content-Type', 'application/json', true); 
$this->response->sendHeaders(); 

如果您通過專用的pagetype訪問你的數據,你可以設置頁眉這一pageType爲何在Typo腳本:

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

我將它添加到我的文章的話題:https://usetypo3.com/json-view.html