[英]Ajax call VERY SLOW with PHP
我正在使用Ajax使用Twitter Bootstrap選項卡加載div中另一個頁面的內容,但是ajax加載頁面的時間太長。
沒有Ajax頁面加載速度非常快!
在ajax調用中加載的頁面:28.743376016617 ms
頁面加載沒有ajax:0.00022506713867188毫秒
這是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,您可以加載更少的數據,這樣它的運行速度就會快。
如果加載的數據不常見,則通過異步腳本加載。 在頁面加載后幾秒鍾加載ajax div。
如果這需要很長時間才能加載,請取消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]
}
});
我有同樣的問題,並在添加標題后:
header("Content-Length: xxx")
它工作得非常快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.