Ajax call VERY SLOW with PHP


Ajax call VERY SLOW with PHP

我正在使用Ajax加载带有Twitter Bootstrap选项卡的div中另一个页面的内容,但Ajax加载页面的时间太长
没有Ajax页面加载速度非常快!

ajax调用中加载的页面:28.743376016617 ms
无ajax加载的页面:0.00022506713867188 ms

这是ajax调用的代码:

    $(function() {
    $("#MainTabs").tab();
    $("#MainTabs").bind("show", function(e) {
      var contentID  = $(e.target).attr("data-target");
      var contentURL = $(e.target).attr("href");
      if (typeof(contentURL) != 'undefined')
    $(contentID).html('<img src="<?php echo IMG_DIR; ?>loading/loading-large.gif" width="64" />').load(contentURL, function(){
        $("#MainTabs").tab();
    });
      else
    $(contentID).tab('show');
    });
    $('#MainTabs a:first').tab("show");
}); 

这是一个PHP代码:

<?php
$start = microtime(TRUE); // Start counting
ob_start();
session_start();
$temp = microtime(TRUE) - $start;
echo $temp;
exit;
/*
 * Here is the rest of the contents of the script, so I gave the 'exit' and even with the exit delay it that way!
*/

有人知道发生了什么事以及如何帮助我吗?PHP代码非常简单,而且耗时太长!

谢谢!

您的Ajax是否从需要时间才能生成html的后端加载html?

如果没有Ajax,加载的数据就会减少,所以它的运行速度很快。

如果加载的数据不太常见,则通过异步脚本进行加载。页面加载几秒钟后加载ajaxdiv。

  1. 如果加载需要很长时间,请取消ajax请求。

    $(document).ready(
    var xhr;
    var fn = function(){
        if(xhr && xhr.readyState != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };
    var interval = setInterval(fn, 500);
    

    );

请使用.ajax()调用而不是.load().

试试这个:

$(contentID).html('<img src="<?php echo IMG_DIR; ?>loading/loading-large.gif" width="64" />');
$(contentID).ajax(
   url: contentURL, 
   type: "GET",//or POST
   success: function(data){
     $(contentID).html(data);
     console.log(data);//it will show the error log if any [optional]
   }
});

请在此处查看ajax调用

我遇到了同样的问题,在添加标题后:

header("Content-Length: xxx")

它工作得很快。