<html>
<head>
<script type="text/javascript">
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)
{
//readyState的值表示了当前请求的状态,在事件处理程序中可以根据这个值来进行不同的处理
// tatus存储了服务器端返回的Http请求响应代码,它表示请求的处果。
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/ajax/demo_get.asp?t=" + Math.random(),true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
本文介绍了一种利用AJAX技术实现网页局部刷新的方法。通过一个简单的示例展示了如何使用JavaScript编写AJAX请求来从服务器获取数据,并更新指定的DOM元素内容。此方法能有效提升用户体验,减少不必要的页面刷新。
1349

被折叠的 条评论
为什么被折叠?



