Date: 6/16/2025Category: frontendTag: http, frontend, javascript
原生请求
// 发起GET请求
const xhr = new XMLHttpRequest;
xhr.open('GET', 'http://127.0.0.1:3000?name=tom&password=123456')
xhr.onload = () => {
console.log(xhr.status)
if (xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log(`Error:${xhr.status}`)
}
}
xhr.send();
// 发起POST请求
const xhr = new XMLHttpRequest;
xhr.open('POST', 'http://127.0.0.1:3000/post/userInfo');
xhr.setRequestHeader('Content-type', 'application/x-www-formurlencoded');
xhr.onload = () => {
if (xhr.status = 200) {
console.log(xhr.responseText);
} else {
console.error(`Error:${xhr.statis}`);
}
};
xhr.send(JSON.stringify({
name: "12312321",
email: "131123@qq.com"
}))