<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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","demo.php?rand=" + Math.random(),true);
//为了解决缓存问题,在URL添加唯一id.
//但是不太明白是如何实现的,文件名就是demo.php加上 "?rand="之后是怎么找到原来的文件的?
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
对于后端的
demo.php
, 收到的是通过 URL 参数传递给当前脚本的变量的数组$_GET
?分割的是一些键=值, 作为get的参数发送
http://www.w3school.com.cn/tags/html_ref_httpmethods.asp