1.创建异步请求对象XMLHttpRequest
var xhr = new XMLHttpRequest()
2.使用open方法与服务器建立连接
get请求:
xhr.open(“get”,“请求的url”,true)
post请求:
xhr.open(“psot”,“请求的url”,true)
xhr.setRequestHeaders(“content-Type”,“application/x-www-form-urllencoded”)
3.使用send方法向服务器发送请求
get请求:
xhr.send()
post请求
xhr.send(参数)
4.设置监听,并接受响应
xhr.onreadystatechange=function(){
if(xhr.readState4&&xhr.status200){
//对响应数据做出处理
}
}