【发布时间】:2017-08-31 04:02:09
【问题描述】:
我需要从 php 返回 json 对象,这是我的代码:
$resultFromDB = PerformTransaction($requestToDB);
if($resultFromDB['status'] == '0'){
$myReturn = array('status' => '0','message' => 'success');
header('Content-Type: application/json');
$json_result = json_encode($myReturn,JSON_UNESCAPED_UNICODE);
echo trim($json_result,'');
}else{
$myReturn = array(
'status' => $resultFromDB['status'],
'message' => $resultFromDB['errorMsg']
);
header('Content-Type: application/json');
echo json_encode($return,JSON_UNESCAPED_UNICODE);
}
当函数的结果状态 = 0 时,该代码的结果应该是:
{"status":"0","message":"success"}
当通过 Idea IntellIJ Test RESTful webservice 发送时,我得到了结果:

{"status":"0","message":"success"}
如何正确返回结果,我不会有多余的字符
【问题讨论】:
-
在header('Content-Type: application/json')之前添加函数ob_clean();