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

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路