2015-07-20 2 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

'заголовка ('Content-Type: приложение/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; 
} 
Смежные вопросы