我希望可以使用Ajax在本地访问文件,我在mozilla firefox上尝试了该文件并运行良好。我创建了2个文本文件,并在同一个文件夹中移动。这是代码。抱歉,有任何错误。
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //Not IE
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); //IE
}
else {
alert("Your browser doesn't support the XmlHttpRequest object. Better upgrade to Firefox.");
}
}
var receiveReq = getXmlHttpRequestObject();
function sayHello(fname) {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", fname, true);
receiveReq.onreadystatechange = handleSayHello;
receiveReq.send(null);
}
}
function handleSayHello() {
if (receiveReq.readyState == 4) {
document.getElementById('span_result').innerHTML = receiveReq.responseText;
}
}
Here is the html code
Select a file
file.txt
file2.txt
Ajax.html
Contents of the file will be displayed below