function ajax(url, success) {
// 1. 准备一个电话(创建一个ajax对象)
var xhr = new XMLHttpRequest()
// 2.拨号(连接到服务器)
xhr.open('get', url, true);
// 3.说出你的需求(发送请求)
xhr.send();
// 4.对方的反馈(接收返回值)
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) { // 请求完成
if(xhr.status === 200) { // 成功
success && success(xhr.responseText) // responseText: 响应文本(服务器返回的数据)
} else { // 失败了
console.log('失败了');
}
}
}
}
ajax('data.json', function(data) {
console.log(data);
})