How To Stop Yii From Sending Debug Data In Ajax Request

I’m using uploadify to upload documents and and using Yii’s form handling on the backend. Here’s my current code:




        if (Yii::app()->getRequest()->getIsPostRequest()) {

            $json = [];

            $form = new \InstaLabel\Models\Forms\TemplateUpload();

            if (!$form->validate()) {

                header('HTTP/1.1 500 Internal Server Error', true, 500);

                $json['errors'] = $form->getErrors();

            }

            echo json_encode($json);

            return Yii::app()->end();

        }

I’m running into an issue where my application’s debug information is being sent in addition to the ajax. I can just use exit() to prevent this, but I’d rather have a proper shutdown of the application. I’m not able to find a clear answer on this.

Any advice? In the mean time, I’m using exit()

I don’t know if I fully understand you question and if this will solve your problem.

I think you can use afterValidate method of the CActiveForm. if this method returns false, it stops sending the normal request form then you can do an ajax request in this method. Then you don’t need to do:

header(‘HTTP/1.1 500 Internal Server Error’, true, 500);

Please read these =>

http://www.yiiframework.com/forum/index.php/topic/18460-cactiveform-submitted-with-ajax/page__p__90885__hl__afterValidate+forum#entry90885

http://www.yiiframework.com/doc/api/1.1/CActiveForm (the part of afterValidate)

Exemple =>




<?php

$js=<<<EOD

 js:function(form, data, hasError) {

   //put your instructions here 

 }

EOD;


?>

<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'XXX',

        'enableAjaxValidation'=>true,

        'clientOptions'=>array('validateOnSubmit'=>true,

                             'afterValidade'=>$js ))); ?>

 ...



Really I’m just trying to figure out how to return json data via ajax without all the debug stuff when debugging is enabled

The simple way:

Use exit instead of Yii::app()->end

The hard way: turn off all loggers in case of ajax request.

for example:


foreach (Yii::app()->log->routes as $route) {

    $route->enabled=false;

}

Perfect, thanks!!

Just extending ORey’s answer above, if you warn to turn off all loggers then you could go also for something like:




foreach (Yii::app()->log->routes as $route)

{

         if ($route instanceof CWebLogRoute || $route instanceof CFileLogRoute || $route instanceof YiiDebugToolbarRoute)

         {

                  $route->enabled = false;

         }

}



This is an old issue… See feature request here in forum [1].

Just also opened an issue for this on github [2], maybe it gets in the core one day…


[1] http://www.yiiframework.com/forum/index.php/topic/26038-content-type-aware-cweblogroute/

[2] https://github.com/yiisoft/yii/issues/2165

I know this is old but still to save someone some time:

I use in the controller before the end of ajax call:


			foreach (Yii::app()->log->routes as $route) 

				if ($route instanceof CWebLogRoute) 

 					$route->enabled=false;			


			Yii::app()->end();



This stops CWebLogRoute from adding debug info (like system.db.CDbCommand) at the end of json output, but still allows CFileLogRoute to save Yii::log() in ajax calls