2016-08-01 267 views
0

我做了一個ajax函數調用一個/ pages文件夾中的php頁面,我打電話給我的頁面在ajax-container div,但是當我點擊refresh the page按鈕時,history pushstate正在工作,因爲我在地址欄中看到了頁面,但它僅加載了index.php的內容,但在我的ajax-container中沒有任何反應。 那麼我怎麼能得到我刷新頁面時調用的頁面?Ajax頁面刷新頁面按鈕沒有返回點擊

htacces:

Options +FollowSymLinks 
 

 
RewriteEngine On 
 

 
RewriteBase/
 
RewriteRule ^([a-zA-Z0-9\-]*).php$ index.php?p=$1 [L]

Ajax的容器:

<div id="ajax-container"> 
 
<?php 
 
$d = "pages/"; 
 
if (isset($_GET['p'])) { 
 
    $p = strtolower($_GET['p']); 
 
    if (preg_match("/^[a-z0-9\-]+$/", $p) && file_exists($d . $p . ".php")) { 
 
     include $d . $p . ".php"; 
 
    } else { 
 
     include $d . "404.html"; 
 
    } 
 
} else { 
 
    include $d . "home.php"; 
 
} 
 
?> 
 
</div>

和我的AJAX功能:

var afficher = function(data, page) { 
 

 
    $('#ajax-container').delay(200).fadeOut(250, function() { 
 
     $('#ajax-container').empty(); 
 
     $('#ajax-container').append(data); 
 
     $('#ajax-container').fadeIn(100, function() {}); 
 

 
    }); 
 
}; 
 

 
var lastRequest = null; 
 
if (lastRequest !== null) { 
 
    lastRequest.abort(); 
 
} 
 

 
var loadPage = function(page, storeHistory) { 
 
    if (typeof storeHistory === 'undefined') { 
 
     storeHistory = true; 
 
    } 
 

 

 
    lastRequest = $.ajax({ 
 
     url: "pages/" + page, 
 
     cache: false, 
 
     success: function(html) { 
 
      afficher(html, page); 
 
      if (storeHistory === true) { 
 
       history.pushState({ 
 
        'key': 'value', 
 
        'url': page 
 
       }, '', page); 
 
      } 
 
     }, 
 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
 
      afficher('erreur lors du chagement de la page'); 
 
     } 
 
    }); 
 

 
    return false; 
 
}; 
 

 
window.addEventListener('load', function() { 
 
    setTimeout(function() { 
 
     window.addEventListener('popstate', function(e) { 
 
      if (e.state === null) { 
 
       loadPage('home.php'); 
 
      } else { 
 
       loadPage(e['state']['url'], false); 
 
      } 
 
     }); 
 
    }, 0); 
 
});

+0

嗯,我只是很熟悉純粹的JavaScript AJAX的頂部,你想要做什麼困惑。然而,「loadPage」看起來像你想要去一個新的頁面? ... AJAX不打算進入一個新的頁面,它帶來的內容,而無需重新加載頁面。 – Tyler

+0

是的,我想做什麼它在我的index.php我帶'about.php'在我的ajax容器,所以現在在我的'about.php'我有另一個鏈接,我想當我點擊這個鏈接, AJAX用這個新頁面替換'about.php' – Tiaw

回答

0

我明白了,那麼我不明白jQuery的,因爲我只用純JavaScript AJAX工作。但是,我知道一些事情要檢查。

1-您的PHP是否迴應了事情?即使你通過AJAX獲得一個php文件,它也不會顯示任何東西,除非信息被回顯出來。

2 - 我沒有看到innerHTML,與我的經驗(再次只使用純JavaScript AJAX)一樣,回顯的php代碼將放在div的innerHTML中。例如你的div: 阿賈克斯前:

<div id="ajax-container"> 
    hello 
</div> 

AJAX得到php文件:

AJAX完成,內容是準備好...(XMLHTTP =的XMLHttpRequest())

document.getElementById("ajax-container").innerHTML=xmlhttp.responseText;//responseText= whatever php echoed out 

在AJAX之後:

<div id="ajax-container"> 
    hi 
</div> 

這取代了div的內容T,下面將添加到它:

document.getElementById("ajax-container").innerHTML.=xmlhttp.responseText; 
         note the period for concat^ 

3-如果你仍然有問題,打開你的瀏覽器開發者工具 - >網絡標籤 - > php文件名

這將讓你看到什麼該PHP文件是說(var_dumps,回聲,錯誤,如果錯誤報告的東西允許它)