yii框架上的ajax响应非常慢


Very slow ajax response on yii framework

我对yii框架有一个奇怪的问题。在localhost上,ajax响应需要200ms(这很快,我很满意),而在我的实时服务器上,同样的功能需要4到7秒。

下面是我的php ajax函数:-

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代码为:-

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

*注意:-到目前为止,数据库只有10到20条记录。此外,在实时服务器上,我所有的ajax调用都会导致响应缓慢

我会尝试一些事情。首先,在你echo你的json之后,我会杀死你的脚本,以确保没有其他东西运行:

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

此外,在您的index.php上,确保您已将站点从调试模式中删除,如果您启用了以defined()开头的中间两行中的任何一行,则每次页面加载Yii都会重新创建缓存文件,这可能需要一段时间,尤其是当您包含bootstrap等扩展时。我在为某人做一些工作时遇到了这个问题,他们的网站是在GoDaddy上托管的。由于某种原因,文件创建非常缓慢,而且拖得很慢。

<?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();

还有其他功能运行缓慢吗?错误日志中有任何错误吗?

另一个帮助调试的选项创建了另一个不需要AJAX调用的操作。用这种方式调试比依赖ajax要容易得多,而且它可以帮助您缩小问题的根源。另外,不知道为什么,但你得到了行数组,然后重新填充行数组,这是非常多余的。

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();
}

然后只需使用浏览器并转到http://yoursite.com/index.php/request/checkpopup?uid=1