创建对象(创建代理对象)
var xhr = new XMLHttpRequest()
console.log(xhr.readyState) // => 0
建立连接指定服务器端口
xhr.open(method, url)
console.log(xhr.readyState) // => 1
发送数据
xhr.send() // 可发送字符串、二进制数据
console.log(xhr.readyState) // => 1
发送请求头数据
var xhr = XMLHttpRequest();
xhr.open();
shr.setRequestHeader(key, value); // 设置请求头
shr.send()
接收返回数据
xhr.addEventListener('load', function(){
console.log(this.response)
})
绑定返回事件机制
xhr.addEventListener('readystatechange', function(){
swith(this.readyState){
case 2:
// 接收服务端的响应报文
case 3:
// 正在接收服务端的响应体
case 4:
// 响应体已全部接收成功
}
})