在php

2015-07-20 27 views
0

中將HTTP內容類型響應設置爲「application/json」我有問題。我有如下因素條件:在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提前

+0

'標題(「內容類型:應用程序/ json');' –

+0

我需要添加這個'header' – TanGio

+0

我想你想要這個 - http://stackoverflow.com/questions/4162223/how-to-send-500-internal-server-error-error -from-a-php-script,並且不需要返回。簡單地退出;標題足夠後。 – ArtisticPhoenix

回答

1

你需要輸出的JSON(不僅僅是歸還),並讓客戶知道內容是JSON通過設置Content-Type

// 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; 
}