yii框架上的Ajax响应非常慢 [英] Very slow ajax response on yii framework

查看:59
本文介绍了yii框架上的Ajax响应非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对yii框架有一个奇怪的问题.在本地主机上,Ajax响应需要200毫秒(这让我感到非常快),而在我的实时服务器上,相同的功能需要4到7秒钟.

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.

以下是我的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调用都给了我较慢的响应.

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

推荐答案

我会尝试一些方法.首先,在您 echo 您的json后,我将杀死您的脚本以确保没有其他内容运行

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

如果您有以 defined()开头的中间两行之一,还请确保在 index.php 上使站点退出调试模式启用每个页面加载Yii会重新创建缓存的文件,这可能需要一段时间,尤其是如果您包含扩展名(如引导程序).当为某人做一些工作时,我遇到了这个确切的问题,他们的网站托管在GoDaddy上.由于某种原因,文件创建的速度确实很慢,并且确实拖累了所有内容.

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?

另一个有助于调试的选项,可以创建不需要AJAX调用的其他操作.以这种方式调试而不是依靠ajax进行调试要容易得多,此外,它还可以帮助您缩小问题的根源.另外,不知道为什么,但是先获取行数组,然后重新填充行数组,这是非常多余的.

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

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

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

这篇关于yii框架上的Ajax响应非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆