黑马ajax总结
/**
* 目标:使用XMLHttpRequest对象与服务器通信
* 1. 创建 XMLHttpRequest 对象
* 2. 配置请求方法和请求 url 地址
* 3. 监听 loadend 事件,接收响应结果
* 4. 发起请求
*/
//1. 创建 XMLHttpRequest 对象
const xhr = new XMLHttpRequest()
//2. 配置请求方法和请求 url 地址
xhr.open('GET','http://hmajax.itheima.net/api/province')
//3. 监听 loadend 事件,接收响应结果.loadend无论成功与否都会执行回调函数
xhr.addEventListener('loadend',()=>{
//xhr中有一个固定的属性,用来接收服务器返回的数据,这个名字时固定的不能修改
console.log(xhr.response)
JSON.parse(xhr.response)
})
//4. 发起请求.使用内置的send()方法发送请求
xhr.send()