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(); // 发送请求