我是AJAX新手,嘗試使用它來加速顯示PHP全文搜索的結果。我有大約1700個文件需要搜索,所以不用等待服務器處理所有我想發送的第100個腳本並顯示結果,然後再顯示下一個100等,這樣用戶就可以立即獲得滿足感。AJAX問題:多個不同的請求返回相同的響應
爲此,我調用函數callftsearch,其中包含字符串中所有文件的名稱以及另一側的PHP函數運行搜索所需的一些其他信息。 callftsearch創建每個100個文件的數組,將它們加入到字符串中,並通過javascript函數ftsearch將其發送到ftsearch.php。 PHP運行搜索並格式化結果以供顯示,並將HTML字符串與表格返回。 addresults()只是將該表追加到頁面上的現有div上。
這裏的JavaScript:
function GetXmlHttpObject()
{
var xmlHttp=null;
try { xmlHttp=new XMLHttpRequest(); }
catch (e) { try { xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e) { xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); } }
return xmlHttp;
}
function callftsearch(allfiles, count, phpfilenames, keywordscore, keywordsboolean, ascii) {
var split_files = allfiles.split("|");
var current_files = new Array();
var i;
for (i = 1; i<=count; i++) {
file = split_files.shift();
current_files.push(file);
if (i%100 == 0 || i == count) {
file_batch = current_files.join('|');
ftsearch(file_batch, phpfilenames, keywordscore, keywordsboolean, ascii);
current_files.length = 0;
}
}
}
function ftsearch(file_batch, phpfilenames, keywordscore, keywordsboolean, ascii)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) { return; }
// If our 'socket' has changed, send the response to addresults()
xmlHttp.onreadystatechange=addresults;
xmlHttp.open('POST','ftsearch.php', true);
var content_type = 'application/x-www-form-urlencoded';
xmlHttp.setRequestHeader('Content-Type', content_type);
xmlHttp.send('f='+file_batch+'&pfn='+phpfilenames+'&kw='+keywordscore+'&kwb='+keywordsboolean+'&a='+ascii);
}
function addresults()
{
var displayarray = new Array();
if (xmlHttp.readyState==4)
{
var ftsearchresults = xmlHttp.responseText;
$('#result_tables').append(ftsearchresults);
}
}
的問題:頁面顯示完全相同的表反覆,只有前幾個結果。當我添加alert(file_batch)到callftsearch它表明它正在連續發送正確的文件包。但是,當我在分析結果()中提醒(ftsearchresults)時,它顯示它一遍又一遍地接收相同的字符串。我甚至在一點上添加了一個時間戳,而且所有打印的表格都是一樣的。
爲什麼會發生這種情況?
如果您不得不搜索1700個文件內容,您應該考慮索引文件內容 – 2011-01-27 15:42:13