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);
}
}
}
링크를 클릭하면 message.txt 파일의 내용으로 새 페이지로 이동합니다. – Colin747
콘솔에있는 오류는 무엇입니까? – koenpeters
질문이 확실하지 않습니다. 오류 메시지가 나타 납니까? 의도 한 행동/입수 한 행동은 무엇입니까? –