AJAX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
oBtn.onclick = function(){
//1.创建AJAX对象
if(window.XMLHttpRequest){
//非IE6
var oAjax = new XMLHttpRequest();
}
else{
//兼容IE6
var oAjax = new ActiveXObject("Microsoft.XMLHttp");
}

//2.连接服务器
//open(方法,文件名,异步传输true/同步传输false)
oAjax.open('GET','data.json',true);

//3.发送请求
oAjax.send();

//4.接收返回
oAjax.onreadystatechange = function(){
//已经接收到全部响应数据
if(oAjax.readyState == 4){
if(oAjax.status == 200){
console.log("成功"+oAjax.responseText);
}
else{
console.log("失败");
}
}
}
}
}