Ajax使用
1.实例化一个XMLHttpRequest对象
2.设置open()参数
3.设置状态改变函数响应 onload() or onreadystatechange()
4.发送请求 send()
下面是一个典型的get请求
<button id="button"> 请求纯文本</button>
<script>
document.getElementById('button').addEventListener('click',loadtext);
function loadtext(){
//创建XMLHttpRequset对象
var xhr = new XMLHttpRequest();
xhr.open('GET','sample.txt',true);
console.log(xhr.readyState);
//两种方式设置响应操作 onload / onreadystatechange
// onload只有在状态值为4才执行
//onreadystatechange在状态码改变的时候就触发
xhr.onreadystatechange = function(){
console.log(this.status+" or "+this.readyState);
if(this.status=200 && this.readyState ==4)
console.log(this.responseText);
}
//发送请求
xhr.send();
}
</script>
请求多个JSON
<!DOCTYPE html>
<