根據w3schools.com XMLHttpRequest對象的開口的方法有三個參數: 1.方法 2. URL 3.一個布爾值,指示該呼叫是否是異步或不xmlhttp.open(「GET」,「ajax_info.txt」,true):我錯過了什麼?
其中一個例子中使用的文本文件的url參數。我複製了代碼,並用自己的文本替換了文本文件,沒有發生任何事情。我錯過了什麼?
這是我複製的代碼:
這不叫XMLLocalFileRequest :)
嘗試使用http://localhost/ajax_info.txt
爲URL○:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
請原諒我的無知我是一個noob。我使用了:「\ http://localhost/ajax_info.txt」,並沒有發生任何事情。謝謝您的幫助。 – MR1971 2014-09-22 08:59:05