繁体   English   中英

这段代码有什么问题? (阿贾克斯)

[英]What is wrong with this code? (AJAX)

我的代码似乎不起作用,即与服务器通信。 有人可以告诉我我犯了什么样的错误吗? 我监督了什么吗?

PS – 新的 AJAX。

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(){
var xmlhttp;

if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
}

else {
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){

if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("mySection").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET","ajax_info.txt",true);

xmlhttp.send();

}
</script>
</head>

<body>
<section id="mySection">
<h2>Let AJAX change this text</h2>
</section>
<button onclick="loadXMLDoc()" type="button">Change Content</button>

</body>
</html>

你的代码非常好。 只需检查您作为xmlhttp.open("GET","ajax_info.txt",true); 中的第二个参数传递的网址 . 很可能您传递的文件名不正确。

要检查您的代码是否工作正常,或者只是将ajax_info.txt url 替换为https://api.github.com/users/octocat或任何其他可公开访问的 api url 或任何本地存储文件的名称(只需确保下次传递正确的名字)。

“与服务器通信”:基本上,在这里您传递的是本地文件名,这就是它不与服务器通信的原因。 为了让它与服务器通信,传递实际的 url,就像上面 Github url 一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM