2012-05-17 127 views
1

我想獲得一個AJAX示例工作,但我無法讓它工作。你能在XAMPP上運行嗎?AJAX不在本地主機上工作

我有三個文件,message.txt,index.html,ajaxtest.js。當你點擊超鏈接時,它會彈出message.txt的內容。 (所有文件都在同一個目錄)

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html> 
    <head> 
    <title> Using XMLHttpRequest</title> 
    <script type="text/javascript" src="ajaxtest.js"></script> 
    </head> 
    <body> 
    <p> 
     <a href="message.txt" onclick="grabFile(this.href); return false;"> 
Click here 
</a> 
</p> 
</body> 
</html> 

ajaxtest.js

function getHTTPObject() { 
    var xhr = false; 
    if (window.XMLHttpRequest) { //see if a XMLHttpRequest exists 
    xhr = new XMLHttpRequest(); //if it exists change "xhr" to a new instance of the object 
    } 
    else if (window.ActiveXObject) { //see if ActiveX exists (for IE) 
    try { //Allows newer versions of IE to use the newer ActiveX object 
     xhr = new ActiveXObject("Msxml2.AMLHTTP"); //if it exists change "xhr" to a new instance of the object 
    } 
    catch(e) { //catches an error and resets "xhr" to false 
     try { //Allows older versions of IE to fall back onto the older version using "try...catch" 
     xhr = new ActiveXObject("Microsoft.XMLHTTP"); //if it exists change "xhr" to a new instance of the object 
     } 
     catch(e) { 
     xhr = false; 
     } 
    } 
    } 
    return xhr; 
} 

function grabFile(file) { 
    var request = getHTTPObject(); 
    if (request) { 
    request.onreadystatechange = function() { 
     displayResponse(request); 
    }; 
    request.open("GET", file, true); 
    request.send(null); 
    } 
} 

function displayResponse(request) { 
    if (request.readyState == 4) { 
    if (request.status == 200 || request.status == 304) { 
     alert(request.responseText); 
    } 
    } 
} 
+0

當您點擊鏈接時,它的行爲完全一樣,並帶我進入一個新的頁面,其中包含message.txt文件的內容 – Colin747

+0

控制檯中出現的錯誤是什麼? – koenpeters

+0

你的問題不清楚。你有錯誤信息嗎?什麼是預期的行爲/得到的行爲? –

回答

0

正如我曾嘗試與你的榜樣,您的申請對象不打州4和再次使用。 在下面的代碼之前檢查request.status,它總是得到0值。這就是爲什麼你的腳本不提醒響應。

if (request.status == 200 || request.status == 304) { 
    alert(request.responseText); 
} 

選中此爲您的解決方案:http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/

希望可以幫助。

4

出於安全原因,JavaScript的客戶端上的文件系統的訪問受到限制!你可以read more here

如果你正在使用谷歌瀏覽器,你有-allow-file-access-from-files命令來啓動可執行文件:

chrome.exe -allow-file-access-from-files 

有可能是針對Firefox

更新的類似配置: 審查你的代碼,我您正在比較request.statusOK(200)和Not Modified(304)標題的數字!這是HTTP響應頭從Web服務器返回的,所以當你運行一個Web服務器,你必須檢查響應報頭的代碼,否則,在使用file:///協議在本地運行,你總能得到0作爲響應報頭

這裏有一個解決方法,您可以通過檢查域,如果您是在本地運行,則無需檢查響應狀態:

function displayResponse(request) { 
    if (request.readyState == 4) { 
    if (!document.domain || request.status == 200 || request.status == 304) { 
     alert(request.responseText); 
    } 
    } 
} 
+0

在Linux中我該怎麼辦?我試過在終端上輸入「google-chrome -allow-file-access-from-files」,但是當我點擊鏈接時,我仍然有問題,而不是顯示內容的彈出窗口,文件內容。 – Colin747

+0

你需要在執行新的命令行之前關閉所有谷歌瀏覽器的實例 –

+1

剛剛在Windows命令行中測試過,「啓動chrome.exe -allow-file-access-from-files」工作正常 –

相關問題