let xhr=new XMLHttpRequest(); //创建Ajax
XML是一个数据结构
//添加专属事件
xhr.onreadystatechange=()=>{
//判断是否满足Ajax状态码到4(完成)和HTTP状态码为200(成功)
if(xhr.readyState===4&&xhr.status===200){
console.log(xhr.responseText);//输出users.js中res.send()的结果
}
}
//请求的类型 和 请求的地址(URL)
xhr.open("GET",`/users/login?username=${username}&password=${password}`);
xhr.send();//发送请求
请求类型
GET
xhr.open("GET",`/users/login?username=${username}&password=${password}`);
xhr.send();
//在服务端获取内容用req.query
POST
xhr.open(`POST`, `/users/login`);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify({username,password}));
//在服务端获取内容用req.body