gpt4 book ai didi

javascript - 第一个 AJAX 示例无法运行

转载 作者:行者123 更新时间:2023-12-03 04:36:58 24 4
gpt4 key购买 nike

我已经开始学习 W3Schools 网站上的 AJAX 教程,但我的第一个示例甚至无法运行。请问这个为什么不运行?它在浏览器中打开,但当我单击按钮时没有任何反应。

教程网址;

https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_ie

这是我在 Notepad++ 中创建的 HTML 页面

<!DOCTYPE html>
<html>
<body>

<h1>The XMLHttpRequest Object</h1>

<p id="demo">Let AJAX change this text.</p>

<button type="button" onClick="loadDoc()">Change Content</button>

<script>
function loadDoc() {
var xhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhttp = new XMLHttpRequest();
} else {
// code for IE6 and IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>

我通过在 Notepad++ 中选择“运行 >> chrome”来运行 HTML 页面(但也尝试了 Firefox 和 IE)。 ajax_info.txt 文件与 HTML 文件位于同一位置。这是其内容;

AJAX 不是一种编程语言。

AJAX 是一种从网页访问 Web 服务器的技术。

AJAX 代表异步 JavaScript 和 XML。

最佳答案

您必须更改 xhhtp.onreadystatechange 而不是 xhttp.onreadystatechangexhttp.send(); 而不是 xhttp.send ();

function loadDoc() {
debugger;
var xhhtp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhhtp = new XMLHttpRequest();
} else {
// code for IE6 and IE5
xhhtp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhhtp.open("GET", "ajax_info.txt", true);
xhhtp.send();
}

关于javascript - 第一个 AJAX 示例无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43253495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com