2012-05-17 7 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

. 아래 코드 앞에 request.status를 확인하십시오. 항상 0 값을 얻습니다. 그리고 그것이 스크립트가 응답을 경고하지 않는 이유입니다. 솔루션에 대한

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

확인이 : 도움이 http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/

희망.

4

클라이언트의 파일 시스템에 자바 스크립트의 액세스가 제한됩니다 (모든 파일이 같은 디렉토리에 있습니다)!

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

파이어 폭스

갱신을위한 유사한 구성이있을 수 있습니다 : 당신은 당신이 -allow-file-access-from-files 명령을 실행 파일을 시동하기 위해 구글 크롬을 read more here

한 사용하는 경우 코드를 검토 , 난 OK (200) 및 Not Modified (304) 헤더와 비교하는 수치는 request.status입니다! 이들은 HTTP 응답 헤더가 웹 서버에서 반환, 그래서 당신이 그렇지 않으면, 웹 서버 당신 해야 확인 응답 헤더의 코드를 실행할 때, 때 당신은 항상 응답 헤더

에 관해서는 0을 얻을 file:/// 프로토콜을 사용하여 로컬로 실행 여기에 해결 방법입니다 당신이 다음 로컬 응답 상태를 점검 할 필요가 실행되지 않은 경우, 당신은 도메인을 확인할 수 있습니다 : 나는 당신의 요청 객체가 상태 (4)을 타격하고 다시 사용하지 않는, 당신의 예제를 시도으로

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

새 명령 줄을 실행하기 전에 google chrome의 모든 인스턴스를 닫아야합니다. –

+1

Windows 명령 줄에서 'start chrome.exe -allow-file-access-from-files' 테스트를 그냥 잘 수행했습니다. –

관련 문제