代码中是否有任何错误,代码是从w3schools中拷贝来练习的。当我点击我的笔驱动器按钮我没有重定向到页面mobile.txt 有问题吗?无法在chrome,IE或firefox上运行ajax代码
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">pendrive</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "mobile.txt", true);
xhttp.send();
}
</script>
</body>
</html>
首先 - 您在浏览器控制台中遇到什么错误。 ?第二个AJAX调用用于将数据加载到页面上,而不是将您重定向到另一个页面,如果您希望这样做,您可以使用'window.location.href =“http://someurl.com” –