Yii2 报错 Headers already sent in

有时候我们把Yii2 版本进行升级之后 会报一些莫名其妙的错误,这次就遇到如下错误信息

an Error occurred while handling another error:
exception 

'yii\web\HeadersAlreadySentException' 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): yii\web\Response->sendHeaders()
#1 /xxxx/xxxx/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2/xxxx/xxxx/vendor/yiisoft/yii2/base/ErrorHandler.php(111):

yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))

#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#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 服务常见的几个错误解决方案

原文地址:https://www.cnblogs.com/apanly/p/12446266.html

时间: 2024-10-23 09:33:51

Yii2 报错 Headers already sent in的相关文章

yii2 HeadersAlreadySentException 报错

An Error occurred while handling another error:exception 'yii\web\HeadersAlreadySentException' 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/vend

关于报错:Warning: Cannot modify header information - headers already sent by (output started at

8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers already sent by (output started at 经过再三在百度上查询,终于查到错误的关键原因:[setcookie之前不可以有html标签内容].导致出现这种错误的方式有很多种,但惟独这种解释最让我理解. (因为百度知道的内容不可以复制了,只能贴出地址,有详细解释)地址[http://z

YII2安装和更新报错

报错内容如下: Your requirements could not be resolved to an installable set of packages. Problem 1 - yiisoft/yii2 2.0.x-dev requires bower-asset/jquery.inputmask ~3.2.2 -> nomatching package found. - yiisoft/yii2 dev-master requires bower-asset/jquery.inpu

iOS 真机测试的时候报错 After modifying system headers, please delete the module cache at '/ /Users/smile/Libr

这个是Xcode缓存导致的 fatal error: file ' '/ /Users/smile/Library/Developer/Xcode/DerivedData/ModuleCache/VE5HTVL861UA' was built note: after modifying system headers, please delete the module cache at '/Users/ligang/Library/Developer/Xcode/DerivedData/Modul

安装编译qemu-kvm报错Make sure to have the zlib libs and headers installed.

[[email protected] qemu-kvm.git]# ./configure Error: zlib check failed Make sure to have the zlib libs and headers installed. 解决上面报错需要安装zlib相关的软件 [[email protected] qemu-kvm.git]# yum install zlib* 安装完上面软件后继续出现下面报错 [[email protected] qemu-kvm.git]# .

php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0

php5.6ajax报错 Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unkno

Dockerfile里pip.conf报错ConfigParser.MissingSectionHeaderError: File contains no section headers.

Dockerfile的部分内容如下: RUN mkdir /root/.pip && echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple" > /root/.pip/pip.conf RUN pip install PyCryptodome 目测是没有问题,可是docker build的时候就报错: ConfigParser.MissingSectionHeaderErro

解决Win8下安装yii2时,运行php Composer出现SSL报错的问题

今天尝试在win8下运行composer却出现SSL报错: D:\data\www\mmoyu\symapp>php -f %phprc%\composer install Loading composer repositories with package information [Composer\Downloader\TransportException] The "https://packagist.org/packages.json" file could not be

yii2 联系我们发送邮件报错

为什么会报错,因为国内的邮件服务商要求发送邮件的人和设置的smtp服务器账号要相同,因为联系我们的是用户,也就是发件人是用户,而不是我们配置的邮箱,所有出错. 这里我用了个取巧的办法,发件人改为自己,客户的邮箱信息直接拼接在邮件内容,这样我们收到邮件就能知道谁留下的信息.这样有个好处,自己发邮件给自己,成功概率100% 把控制器改为这样写: public function actionContact() { $model = new ContactForm(); if ($model->load