我正试图快速掌握AJAX和JQuery并遵循W3Schools网站上的教程。在tutorial这里按钮被suppossed让AJAX改变文本文本从文件它不会在我的电脑上工作。在与HTML文件相同的位置,我有一个名为“ajax_info.txt”的文件,其中包含以下文本 -为什么不会AJAX更改文本
AJAX不是一种新的编程语言。
AJAX是一种用于创建快速和动态网页的技术。
这是我的代码,为什么它不工作?
<!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>
</body>
</html>
什么是错误? – sumit
为什么不使用jQuery AJAX呢? –
您是否检查过请求状态为“4”且状态为“200”? –