#每日一题# 手写原生ajax,post请求

const xhr = new XMLHttpRequest();
xhr.open('POST', 'example.com');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ name: 'John', age: 30 }));

xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error('请求失败');
}
}
};
展开
1