簡體   English   中英

在php中將HTTP內容類型響應設置為“ application / json”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM