2016-05-09 24 views
0

我在AJAX中練習,我被一個html頁面卡住,該頁面響應用戶點擊按鈕,顯示特定的html文件;XMLHttpRequest.open()中的參數url AJAX

這是我的代碼:

<!DOCTYPE hmtl> 
<html> 
<body> 
    <button type="button" name="button1" >button1</button> 
    <button type="button" name="button2" >button2</button> 
    <button type="button" name="button3" >button3</button> 
    <button type="button" name="button4" >button4</button> 
    <hr/> 
    <p id="demo">visualize document HERE!</p> 
</body> 

<script> 

var documenti= document.getElementByTagName("button"); 
for(var i=0; i<documenti.length; i++) { 
    documenti[i].onclick= loadDoc; 
} 

function loadDoc() { 
    var httpreq= new XMLHttpRequest(); 
    httpreq.onreadystatechange= caricaDocumento; 
    httpreq.open("GET", "this is the file I'm trying to visualize!" ,   true); 
    httpreq.send(); 
} 

function handleResponse(e){ 
    if(e.target.status==200 && e.target.readyState==XMLHttpRequest.DONE) { 
     document.getElementById("demo").innerHTML= e.target.rensponseText; 
    } 
} 
</script> 
</html> 

,如果我想使用一個HTML文件,該文件是在我的電腦,我怎麼可以指定它的屬性網址???如果我正確理解,url參數是我需要服務器的文檔的url,但是如果此文檔不駐留在服務器上?

回答

1

如果我想使用我的電腦上的html文件,我怎麼能指定它在屬性url?

您需要一個URL。

。這可能是一個file:方案URL(其中許多瀏覽器阻塞的XMLHttpRequest出於安全原因),或者,如果你在計算機上運行一個Web服務器,一個http:(或https:)URL。

後面的選項更可靠,所以選擇一些網絡服務器軟件並使用它。