代码
const xhr = new XMLHttpRequest
xhr.open('GET', './data/test.json', true)
xhr.send(null)
xhr.onreadystatechange = () => {
console.log(xhr.readyState)
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(JSON.parse(xhr.responseText))
} else if (xhr.status ===404) {
console.log('404 not found')
}
}
}
结果: