<div id="new"></div>
<button id="btn" onclick="changeContent('C:\Users\MarshMellow\Desktop\whatever.txt', newFunc)">Click Here!!</button>
<script type="text/javascript">
function changeContent(url,callFunc) {
var xhttp;`declaration'
xhttp.onreadystatechange= function() {
if(this.readyState == 4 && this.status == 200) {
callFunc(this);
}
};
xhttp.open("GET","C:\Users\MarshMellow\Desktop\whatever.txt",true);
xhttp.send();
}
function newFunc(xhttp) {`Function call`
document.getElementById("new").innerHTML=xhttp.responseText;
}
仍然沒有輸出。看起來像AJAX呼叫無法正常工作。如何使用AJAX調用訪問任何文本或XML文件?
請張貼的代碼,而不是代碼的圖像爲我工作。你也有任何控制檯中的錯誤? – tommyO
也從你的圖像看起來像'callFunc'被調用,但沒有定義。 – tommyO
您的瀏覽器可能不允許通過XHR加載本地文件。 –