[英]Using Ajax to change the inner content of a div
I am a beginner at Ajax, and I have this html code that is meant to change the inner content of a div by using xmlhttprequest to request different html addresses and put their contents in a div. 我是Ajax的初学者,我有这个html代码,用于通过使用xmlhttprequest来请求不同的html地址并将其内容放在div中来更改div的内部内容。 What am I doing wrong?
我究竟做错了什么? Here is the code:
这是代码:
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
var xmlhttp= null;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
alert("You must be using a different browser.");
}
function makeRequest(serverPage,objID){
var obj = document.getElementById(objID);
xmlhttp.open("GET",serverPage);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 100){
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
</script>
</head>
<body onload="makeRequest ('content1.html','hw')">
<div align="center">
<h1>My Webpage</h1>
<a href="content1.html" onclick="makeRequest('content1.html','hw'); return false;"> Page1</a> | <a href="content2.html" onclick="makeRequest('content2.html','hw'); return false;"> Page2</a> | <a href="content3.html" onclick="makeRequest('content3.html','hw'); return false;"> Page3</a> | <a href="content4.html" onclick="makeRequest(content4.html,hw); return false;"> Page4</a>
<div id = "hw"></div>
</body>
Generally, this looks OK to me. 一般来说,这看起来不错。
However the xmlhttp.status == 100
check looks suspicious. 但是
xmlhttp.status == 100
检查看起来很可疑。
100 is an unusual HTTP status code. 100是一个不寻常的HTTP状态代码。 Typically web servers return 200 ("OK") on a successful request.
通常,Web服务器在成功请求时返回200(“OK”)。
Try replacing the status == 100
check with status == 200
. 尝试使用
status == 200
替换status == 100
check。
For reference,please see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 供参考,请参阅: http : //www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
change xmlhttp.status == 100
to xmlhttp.status == 200
check if this resolves your issue 将
xmlhttp.status == 100
更改为xmlhttp.status == 200
检查这是否可以解决您的问题
and if you are try to run the page in IE try to add this line 如果您尝试在IE中运行该页面,请尝试添加此行
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
Test for xmlhttp.status == 200 instead of 100. 测试xmlhttp.status == 200而不是100。
http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
I guess it should be: 我猜它应该是:
function makeRequest(serverPage,objID){
xmlhttp.open("GET",serverPage);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById(objID).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
function AjaxGet(url,helm)
{
if (xmlhttp == null)
{ 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)
{ if ( (xmlhttp.status==200) || (xmlhttp.status==304) )
{ document.getElementById(helm).innerHTML = xmlhttp.responseText; }
else { document.getElementById(helm).innerHTML = xmlhttp.status + " " +xmlhttp.statusText; }
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.