const sever_url = 'xxx'
let xhr = new XMLHttpRequest()
xhr.open("GET", sever_url, true)
xhr.onreadystatechange = function () {
// 0状态代表未调用open方法 1状态open方法已调用可以通过send发送消息
// 2状态send已经被调用 3状态下载中 4状态下载已经完成
if (this.readyState !== 4) return
if (this.status == 200) {
handle(this.response)
} else {
console.error(this.statusText)
}
}
xhr.onerror = function () {
console.error(this.statusText)
}
xhr.responseType = "json"
xhr.setRequestHeader('Accept', 'application/json')
xhr.send(xxx)
实现AJAX请求
最新推荐文章于 2024-07-26 00:00:00 发布