3 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
出于安全原因,JavaScript對(duì)客戶端上文件系統(tǒng)的訪問(wèn)受到限制-請(qǐng)考慮是否要(其他人的)JavaScript讀取敏感文檔。
即使在進(jìn)行實(shí)驗(yàn)時(shí),最好使用實(shí)際的拓?fù)洌惨獜姆?wù)器上提供服務(wù),而該服務(wù)器將在實(shí)際系統(tǒng)中從那里提供服務(wù)。
設(shè)置一個(gè)Web服務(wù)器(例如Apache)指向您的開(kāi)發(fā)目錄確實(shí)很容易,因此“服務(wù)器”只是您的桌面而已。因此,編輯/測(cè)試周期確實(shí)很快。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
我希望可以使用Ajax在本地訪問(wèn)文件,我在mozilla firefox上嘗試了該文件并運(yùn)行良好。我創(chuàng)建了2個(gè)文本文件,并在同一個(gè)文件夾中移動(dòng)。這是代碼。抱歉,有任何錯(cuò)誤。
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 name="files" onchange="sayHello(this.value)">
<option value="">Select a file</option>
<option value="file.txt">file.txt</option>
<option value="file2.txt">file2.txt</option>
<option value="ajax.html">Ajax.html</option>
</select><br>
<p>Contents of the file will be displayed below</p>
<div id="span_result"></div>
添加回答
舉報(bào)