简体   繁体   English

Ajax用PHP调用非常慢

[英]Ajax call VERY SLOW with PHP

I'm using Ajax to load the content of another page in a div with Twitter Bootstrap tabs, but the ajax is taking too long to load the page. 我正在使用Ajax使用Twitter Bootstrap选项卡加载div中另一个页面的内容,但是ajax加载页面的时间太长。
Without Ajax page loads very fast! 没有Ajax页面加载速度非常快!

Page loaded in ajax call: 28.743376016617 ms 在ajax调用中加载的页面:28.743376016617 ms
Page loaded without ajax: 0.00022506713867188 ms 页面加载没有ajax:0.00022506713867188毫秒

This is a code of the ajax call: 这是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");
}); 

This is a PHP Code: 这是一个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!
*/

Does anyone know what is happening and how to help me? 有谁知道发生了什么以及如何帮助我? The PHP code is very simple and is taking too long! PHP代码非常简单,耗时太长!

Thanks! 谢谢!

Do your Ajax load the html from backend that take time to produce html ? 您的Ajax是否从后端加载了需要时间来生成html的html?

Without Ajax you load less data so that's how its' run fast. 如果没有Ajax,您可以加载更少的数据,这样它的运行速度就会快。

If you load the data that is not so usual then load through Async script. 如果加载的数据不常见,则通过异步脚本加载。 Load the ajax div seconds later after page load. 在页面加载后几秒钟加载ajax div。

  1. cancel the ajax request if this take a long time to load. 如果这需要很长时间才能加载,请取消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); 

    ); );

Please use .ajax() call instead of .load(). 请使用.ajax()调用而不是.load()。

Try this: 尝试这个:

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

Check here for ajax call 点击这里查看ajax电话

I had same issue and after adding headers: 我有同样的问题,并在添加标题后:

header("Content-Length: xxx")

it works very fast. 它工作得非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM