<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
GET 方法
/test/demo_form.asp?name1=value1&name2=value2
请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的
?后面的是参数,请求参数不同,就不会进行缓存。
但是这个参数传到服务器,服务器没有处理就会忽略它,于是就实现了这样的效果