Yii2 报错 Headers already sent in
Posted 即学即码工作室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2 报错 Headers already sent in相关的知识,希望对你有一定的参考价值。
有时候我们把Yii2 版本进行升级之后 会报一些莫名其妙的错误,这次就遇到如下错误信息
an Error occurred while handling another error: exception 'yiiwebHeadersAlreadySentException' with message 'Headers already sent in /xxxx/xxxx/xxx.php on line 90.' in /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php:366 Stack trace: #0 /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php(339): yiiwebResponse->sendHeaders() #1 /xxxx/xxxx/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yiiwebResponse->send() #2/xxxx/xxxx/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yiiwebErrorHandler->renderException(Object(yiiwebHeadersAlreadySentException)) #3 [internal function]: yiiaseErrorHandler->handleException(Object(yiiwebHeadersAlreadySentException)) #4 {main}
查阅了一些资料说,是因为使用json输出内容的时候 没有exit。以前的写法如下
protected function renderJSON($data=[], $msg ="ok", $code = 200) { header('Content-type: application/json'); echo json_encode([ "code" => $code, "msg" => $msg, "data" => $data, "req_id" => $this->geneReqId(), ]); return Yii::$app->end(); }
改成如下
protected function renderJSON($data=[], $msg ="ok", $code = 200) { $response = Yii::$app->response; $response->format = Response::FORMAT_JSON; $response->data = [ "code" => $code, "msg" => $msg, "data" => $data, "req_id" => $this->geneReqId(), ]; return $response; }
原文地址:Yii2 报错 Headers already sent in
标签:yii2 json header
智能推荐
- 【ZBar】ios错误ignoring file xxx missing required architecture x86_64 in file
- Yii2 捕获错误日志
- 【ipv6惹的祸】curl 超时
- Python Flask jsonify a Decimal Error
- Redis 服务常见的几个错误解决方案
以上是关于Yii2 报错 Headers already sent in的主要内容,如果未能解决你的问题,请参考以下文章
处理另一个错误时发生错误:yii\web\HeadersAlreadySentException: Headers already sent
解决Warning: Cannot modify header information - headers already sent by....
PHP cannoy modify header information - headers already sent by ....
PHP错误Warning: Cannot modify header information - headers already sent by解决方法
Warning: Cannot modify header information - headers already sent in Unknown on line 0
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with