ajax随用随看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax案例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function test(){
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){
//alert(xmlhttp.responseText);
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
// xmlhttp.open("GET","/ajax/test.txt",true);
xmlhttp.open("GET","ajax.php?id=10",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>这里的内容会变化哦~~~</h2></div>
<button type="button" onclick="test()">click</button>

</body>
</html>

ajax.php

<?php
$id=$_GET['id'];
if($id==1)
{
echo "11111111111";
}
else
{
echo "22222222222";
}

原文地址:https://www.cnblogs.com/S-Ping/p/4231493.html