2015-05-29 140 views
0

我在網頁中包含一個名爲test.html的HTML頁面。如下圖所示:在HTML頁面中使用jQuery包含HTML頁面

<head> 
    <meta charset="utf-8"> 
    <title>load demo</title> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 

<b>Content I want to Include is here: </b> 
<div id="success"></div> 


<script> 
$("#success").load("test.html", function(response, status, xhr) { 
    if (status == "error") { 
    var msg = "Sorry but there was an error: "; 
    $("#success").html(msg + xhr.status + " " + xhr.statusText); 
    } 
}); 
</script> 

</body> 
</html> 

我得到一個錯誤閱讀:

Sorry but there was an error: 0 NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file://machd/.../HTML/test.html'. 

這個錯誤是什麼意思?以及如何最好地解決它?

+0

這是否[較早前的問題(http://stackoverflow.com/問題/ 28453213 /執行失敗 - 發送xmlhttprequest)幫助? –

+1

你需要一個服務器來使ajax工作。 嘗試xampp或類似的解決方案,如果你還沒有一個 – monxas

+1

本地文件('file:// ...')往往不適用於xhr應用程序,請嘗試本地開發服務器。 – swornabsent

回答

0

我同意上面的swornabsent和monxas。

此代碼在上傳時無誤地運行,並且在服務器上查看頁面。

0

也許你可以試試:

  • 給的絕對路徑的test.html作爲參數,調試
0

它通過文件允許Ajax請求的唯一瀏覽器://協議是舊的IE瀏覽器和由於安全問題而被刪除。您的代碼可以在任何Web服務器(如XAMPP或UWAMP)上正常工作。只需運行Uwamp,把YOUT項目到www文件夾,打開http://localhost

0

簡單的方法...

1>添加JS文件,包括代碼

<script src="https://www.w3schools.com/lib/w3data.js"></script> 

    <script> w3IncludeHTML();</script> 

2>只需撥打像例如HTML文件

<div w3-include-html="h1.html"></div>