在php中将HTTP内容类型响应设置为"application/json" [英] Set the HTTP content type response to “application/json” in php
本文介绍了在php中将HTTP内容类型响应设置为"application/json"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题.我有以下情况:
I have a question. I have the folowing condition :
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();
}
但是不行,请您能帮我吗?提前谢谢
But not work, can you help me please? Thx in advance
推荐答案
您需要输出JSON(而不仅仅是返回它),并通过设置Content-Type
来让客户端知道内容是JSON:
You need to output the JSON (not just return it), and let the client know that the content is JSON by setting the 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;
}
这篇关于在php中将HTTP内容类型响应设置为"application/json"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文