【发布时间】:2020-10-15 08:06:44
【问题描述】:
我正在用 yii2 开发一个 rest api web 服务。 作为 localhost 上的 json 响应,json 响应我的 null 对象结束。 并在主机上出现 json 错误结束。
在 localhost 中,我收到的响应如下: 本地主机上的响应:
{
"status": 1,
"data": {
"data": "Factor Api"
}
}null
但在服务器上,我收到的响应如下: 服务器上的响应:
{
"status": 1,
"data": {
"data": "Factor Api"
}
}<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/factorap/public_html/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response->send()
#2 /home/factorap/public_html/vendor/yiisoft/yii2/base/ErrorHandler.php(135): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(656): yii\web\Response->send()
#2 /home/factorap/public_html/common/components/Api.php(56): yii\base\Application->end()
#3 /home/factorap/public_html/api/controllers/SiteController.php(92): common\components\Api->sendSuccessResponse(Array)
#4 [internal function]: api\controllers\SiteController->actionIndex()
#5 /home/factorap/public_html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#6 /home/factorap/public_html/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
#7 /home/factorap/public_html/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('', Array)
#8 /home/factorap/public_html/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('', Array)
#9 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 /home/factorap/public_html/api/v2/index.php(17): yii\base\Application->run()
#11 {main}</pre>
common/components/Api.php 第 53 行 = $this->setHeader(200);
我发送响应的功能: api函数:
public function sendSuccessResponse($data = false,$additional_info = false)
{
$this->setHeader(200);
$response = [];
$response['status'] = 1;
if (is_array($data))
$response['data'] = $data;
if ($additional_info) {
$response = array_merge($response, $additional_info);
}
$response = Json::encode($response, JSON_PRETTY_PRINT);
//$strpos = strpos($response,'null');
if (isset($_GET['callback'])) {
/* this is required for angularjs1.0 client factory API calls to work */
$response = $_GET['callback'] . "(" . $response . ")";
echo $response;
} else {
echo $response;
}
Yii::$app->end();
}
和要运行的控制器动作。 控制器动作:
public function actionIndex()
{
Yii::$app->api->sendSuccessResponse(['data' => 'Factor Api']);
}
请帮我修复它们。
【问题讨论】:
-
不要回显输出,返回它。
标签: php json yii2 yii2-advanced-app