<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax</title>
</head>
<body>
<button id='but'>ajax的基本使用</button>
<script type="text/javascript">
//获取元素对象
var but = document.getElementById('but');
but.onclick = function()
{
// 1.创建ajax对象
if(window.XMLHttpRequest){
//现在平常使用的浏览器
var x = new XMLHttpRequest();
} else {
//ie5和ie6浏览器使用
var x = new ActiveXObject('Microsoft.XMLHTTP');
}
//2.绑定事件 on 当...时候 readystate 对象属性 change改变
//alert(x.readyState);
x.onreadystatechange = function()
{
//alert(x.readyState);
if(x.readyState == 4 && x.status == 200){
//服务器返回的结果
console.log(x.responseText);
// alert(x.responseText);
}
}
//3.初始化 方法中的参数有3个
// 1.请求的方式(GET POST)
// 2.服务器的文件
// 3.同步还是异步
x.open('GET','1.php',true);
//4.发送
x.send();
}
</script>
</body>
</html>