ajax同步请求的化,需要将响应时间写在发送请求之前
/*
ajax
1 创建ajax对象 同步
2 配置请求信息 同步
3 发送请求 根据参数决定同步\异步,默认是异步
4 绑定接收响应事件 响应时触发
*/
let xhr = new XMLHttpRequest()
// 第三个参数编程false,是同步请求(需要将绑定响应的事件写在发送之前)
xhr.open('get','http://localhost:8888/test/first',false)
// 4 配置一个接收返回信息的事件(onload这种方式不适用低版本ie)
// xhr.onload = function(){
// console.log(xhr.responseText);
// }
// 4.1 使用xhr的onreadystatechange函数
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
console.log(xhr.responseText);
}
}
// 3 发送
xhr.send()