无法在Yi2 api中返回json响应
我试图返回一个无法在Yi2 api中返回json响应,json,api,rest,response,yii2-advanced-app,Json,Api,Rest,Response,Yii2 Advanced App,我试图返回一个JSON响应 $rec = Yii::$app->hescoMDC->createCommand(/** @lang text */ "SELECT * FROM a_data_catalogue d WHERE d.`METER_ASSET_NO` = '$new_msn' AND d.`DISCARD_TIME` IS NULL")->queryOne(); if($rec['CONS_NO'])
JSON
响应
$rec = Yii::$app->hescoMDC->createCommand(/** @lang text */ "SELECT * FROM a_data_catalogue d WHERE d.`METER_ASSET_NO` = '$new_msn' AND d.`DISCARD_TIME` IS NULL")->queryOne();
if($rec['CONS_NO'])
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return Json::encode([
'status' => 'Not OK',
'description' => 'New msn is already attached with a customer ' . $rec['CONS_NO'].' MCO can`t be done'
]);
}
我正在使用
postman
检查响应,但我得到一个空的窗口。首先,当您使用:
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
您不需要使用
Json::encode()
作为回报。只需返回数组您应该显示更多代码以提供更多上下文。除此之外,你确定有什么东西被退回了吗?您是否使用调试器或类似工具逐步完成此操作?最后,您可以通过设置响应格式对json进行双重编码,然后再次显式编码。