2013-06-05 80 views
0

我有一個簡單的頁面,我需要加載HTML文件到一個DIV,但我總有404 NOT FOUND.load()在jQuery的方法給我404 - Not Found錯誤

我稱之爲一個文件demo_test.txt在我的JSP文件相同的文件夾,我需要這個文件加載到帶有ID =「DIV1」

這裏的DIV是代碼:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-latest.min.js"> 
</script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    // $("#div1").load("demo_test.txt"); 
    $("#div1").load("demo_test.txt", function(responseText, statusText, xhr) 
     { 
       if(statusText == "success") 
         alert("Successfully loaded the content!"); 
       if(statusText == "error") 
         alert("An error occurred: " + xhr.status + " - " + xhr.statusText); 
     }); 

    }); 

}); 

</script> 
</head> 
<body> 

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div> 
<button>Get External Content</button> 

</body> 
</html> 
+3

您是否嘗試過使用的URL,瀏覽文件你期望找到它嗎? –

+0

您是否在您的瀏覽器調試器的網絡檢查器選項卡中檢入了哪個文件正試圖加載?然後點擊右鍵並點擊「在新標籤中打開」? –

+0

爲了達到此目的,文件必須與調用腳本的HTML頁面位於完全相同的目錄中。是這樣嗎? – fge

回答

1

像克勞迪奧·雷迪說...

您正在嘗試加載文件r充滿了你所處的道路。嘗試對文件路徑進行硬編碼,然後查看瀏覽器,然後加載它。

所以你的行會是這樣的(如果文件位於WEBROOT/my_app應用/文件)

$("#div1").load("/MY_APP/Files/demo_test.txt", function(responseText, statusText, xhr) 

或完整路徑

$("#div1").load("http://MyWebsite.com/MY_APP/Files/demo_test.txt", function(responseText, statusText, xhr) 
+0

demo_test.txt和jsp頁面在文件夾中的同一個文件夾中:網頁\ WEB-INF \ jsp所以請怎麼寫我的網址 – shehata

+0

我用這個網址「http:/ /localhost:8080/RSS/WEB-INF/jsp/htmlForm.html「,它仍然不起作用 – shehata

+0

嘗試將//放在它的前面,當你不安全時它會從http獲得,而當你是https的時候會從https獲得。所以這個: $(「#div1」)。load(「// localhost:8080/RSS/WEB-INF/jsp/htmlForm.html」,function(responseText,statusText,xhr) –