原生js实现ajax

//发送请求
var xhr=new XMLHttpRequest();
//设置请求报文的请求行
xhr.open('POST','add.php');
//设置请求头
//接收响应
xhr.onreadystatechange=function(){
//获取响应体
if (this.readyState===4) {
console.log(this.responceText);
}
}
xhr.setRequestHeader('info','haha');
//此处需要指明请求头的content-Type,服务器才能按照正确的格式解析请求体
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//设置请求体
xhr.send('key1=value1&key2=value2');

原文地址:https://www.cnblogs.com/ashen1999/p/12559613.html