<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="requestPOST">POST请求</button>
</body>
<script>
requestPOST.onclick = function () {
const oajax = new XMLHttpRequest();
oajax.open("POST", "http://localhost:3000/userInfo", true);
oajax.responseType = "json";
/*
headers 请求头参数 通过设置请求头 向服务端发送数据
content-type 设置发送的数据类型
cookie 设置cookie
token 设置token 添加身份令牌用于服务端验证用户信息
keep-alive 保持长连接
http请求报文
请求方式 请求地址 请求头 请求参数
http响应报文
响应状态码 响应头 响应结果
*/
oajax.setRequestHeader("content-type", "application/json");
oajax.setRequestHeader("token", "AFHLAHFLAHFLAHFLAHF==");
oajax.send(
JSON.stringify({
uid: 10001,
})
);
//请求接口触发的回调函数
oajax.onload = function () {
console.log("获取http响应状态", oajax.status);
console.log("获取http响应状态", oajax.statusText);
console.log("获取响应结果", oajax.response);
};
};
</script>
</html>
01-21
298
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-12
573
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交