[TOC]
XMLHttpRequest
代码演示
const xhr = new XMLHttpRequest()
// GET操作 这里的false代表同步
xhr.open('GET', './text.json', false)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText)
}else{
console.log('其他情况');
}
}
}
xhr.send(null)
// POST操作 这里的true代表异步
/* xhr.open('POST', '/login', true)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText)
}else{
console.log('其他情况');
}
}
}
const postData = {
userName:'zhangsan',
password:'123456'
}
xhr.send(JSON.stringify(postData)) */