- 创建Ajax对象
var nAjax=new XMLHttpRequest()
- 通过open连接到服务器
nAjax.open('请求方式','请求地址',同步或异步)// true 异步 false 同步
注:
这里请求方式如果是post,必须设置header请求头
// 数据格式
nAjax.setRequestHeader('ContentType','application/x-www-formurlencoded')
nAjax.setRequestHeader('Content-Type', 'application/json')
- 通过send发送请求
nAjax.send();
- 接收返回值
nAjax.onreadystatechange=function(){
if(nAjax.readstate==4){// 读取完成
if(nAjax.status==200){// status请求结果
var res=JSON.parse(nAjax.responseText);
console.log(res)
alert("success")
}
}
}