AJAX
原理:
XMLHttpRequest对象
XHR使用步骤
创建XHR对象
const xhr = new XMLHttpRequest()
调用open方法,设置url和请求方法
xhr.open('请求方法','请求的url')
监听loadend事件,接受结果
xhr.addEventListener('loadend',() => {
console.log(xhr.response)
})
设置请求头
xhr.setRequestHeader('Content-Type','application/json')
调用send方法,发起请求
xhr.send(请求体数据)