dougou1127 2013-08-05 10:18
浏览 76
已采纳

对yii框架的ajax响应非常慢

i am having strange issue with yii framework. on localhost, ajax response takes 200ms (which is fast and i am satsified) where as on my live server, same function take 4 to 7 seconds.

below is my php ajax function:-

public function actionOpenpopup() {
                    $this->checkAjaxRequest();                  

                    $user_id = $_GET['uid'];

                    $rows = Yii::app()->db->createCommand()
                              ->select('*')
                              ->from('saved_designs')
                              ->where('uid=:id', array(':id' => $user_id))
                              ->order('date desc')
                              ->queryAll();

                    $i = 0;
                    foreach ($rows as $row) {
                              $rows[$i] = $row;
                              $i++;
                    }
                    if ($rows) {
                              echo json_encode($rows);
                    }
                    else
                              echo json_encode(null);
          }



 function checkAjaxRequest() {
                        if (Yii::app()->request->isAjaxRequest) {
                                  header('Content-Type: application/json; charset="UTF-8"');
                                  return true;
                        } else {
                                  throw new CHttpException('403', 'Forbidden Access');
                                  exit;
                        }
              }

javascript code is:-

function sendAjaxCall(data){

$.ajax({
                                type : 'GET',
                                url : 'index.php/request/openpopup',
                                datatype : 'json',
                                data :data,
                 success: function (data) {
                        console.log(data);                      
                        }
});    

}

*Note:- So far database has only 10 to 20 records. Also On live server, all my ajax calls give me slow response.

  • 写回答

1条回答 默认 最新

  • du9843 2013-08-05 13:08
    关注

    I would try a few things. First off after you echo your json I would kill your script to make sure nothing else runs:

    if ($rows) {
        echo json_encode($rows);
        die();
    }
    

    Also on your index.php make sure you have the site taken out of debug mode, if you have either of the middle two lines that start with defined() enabled each page load Yii is recreating cached files and it can take a while, especially if you have extensions like bootstrap included. I have had this exact issue when doing some work for someone and their site was hosted on GoDaddy. For some reason the file creation was really slow and was really dragging everything.

    <?php
    $yii=dirname(__FILE__).'/../framework/yii.php';
    $config=dirname(__FILE__).'/protected/config/test.php';
    
    //defined('YII_DEBUG') or define('YII_DEBUG',true);
    //defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
    
    require_once($yii);
    Yii::createWebApplication($config)->run();
    

    Also are any other functions running slow? Any errors in your error log?

    Another option to help debug create another action that doesn't require a AJAX call. It is much easier to debug this way instead of relying on ajax, plus it helps you narrow down the source of the problem. Plus don't know why but you get your array of rows then re-populate your array of rows, this is very redundant.

    public function actionCheckpopup() {
        $user_id = $_GET['uid'];
    
        $rows = Yii::app()->db->createCommand()
                ->select('*')
                ->from('saved_designs')
                ->where('uid=:id', array(':id' => $user_id))
                ->order('date desc')
                ->queryAll();
    
        echo json_encode($rows);
        die();
    }
    

    Then simply use a browser and go to http://yoursite.com/index.php/request/checkpopup?uid=1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?