![](/img/trans.png)
[英]PHP API Content-Type: application/json. Response empty data
[英]Set the HTTP content type response to “application/json” in php
我有個問題。 我有以下情況:
You should return an HTTP 500 status code and more details about the error in the message body. Set the HTTP content type response to “application/json”.The error detail must be in JSON format as described below:
{
"ErrorCode" : 402,
"ErrorMessage" : "Item"
}
我這樣嘗試過:
if(!Gain::verifyIfUserExistByIdm($aOutput['UserId'])){
header("HTTP/1.0 500 Internal Server Error");
return json_encode(array('ErrorCode'=>407,'ErrorMessage'=>'Error'));
die();
}
但是沒有用,請你能幫我嗎? 提前Thx
您需要輸出JSON(而不僅僅是返回它),並通過設置Content-Type
讓客戶端知道內容是JSON:
// The user does not exist
if ( ! Gain::verifyIfUserExistByIdm($aOutput['UserId'])) {
// If the user does not exist then "Forbidden" would make sense
header("HTTP/1.0 403 Forbidden");
// Let the client know that the output is JSON
header('Content-Type: application/json');
// Output the JSON
echo json_encode(array(
'ErrorCode' => 407,
'ErrorMessage' => 'Error',
));
// Always terminate the script as soon as possible
// when setting error headers
die;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.