post请求
// post请求
let xhr = new XMLHttpRequest();
xhr.open('post', url);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
// 这步为判断服务器是否正确响应
if (xhr.readyState === 4 && xhr.status === 200 && xhr.response != null && xhr.response !== "") {
// 获取返回信息
let data = JSON.parse(xhr.responseText);
}
}
xhr.send(JSON.stringify(condition)); // 传参
get请求
// get请求
let xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
// 这步为判断服务器是否正确响应
if (xhr.readyState === 4 && xhr.status === 200 && xhr.response != null && xhr.response !== "") {
// 获取返回信息
let data = JSON.parse(xhr.responseText);
}
}
xhr.send(); // 发送请求
本文详细介绍了HTTP中的POST和GET请求方法,包括它们的区别和使用场景。示例展示了如何使用XMLHttpRequest对象进行POST和GET请求,以及处理服务器响应。重点关注了数据传输和交互过程。

2078

被折叠的 条评论
为什么被折叠?



