2023-03-21
xhr ○ readystate
●簡單來說其實就是用一個數字表明了當前 ajax 運行到哪一步了
●語法: xhr.readyState
0: 創建 ajax 成功
1: 當前 ajax 配置成功
2: 當前 ajax 發送成功(響應已經回到瀏覽器了)
3: 表示瀏覽器當前正在解析本次響應, 但可能還沒完成
4: 表示瀏覽器已經完成解析本次響應, 可以正常使用 responseText 了
●0 和 1, 比較好打印, 2/3/4 這幾個我們可以借助一個 事件去打印
○readyStatechange 事件
○通過事件名其實就可以看出, 當 readyState 發生改變時就會執行
const xhr = new XMLHttpRequest()
console.log(xhr.readyState) // 0
xhr.open('GET', 'http://localhost:8888/test/first', true)
console.log(xhr.readyState) // 1
xhr.onreadystatechange = function () {
if (xhr.readyState === 2) console.log(xhr.responseText)
if (xhr.readyState === 3) console.log(xhr.responseText)
if (xhr.readyState === 4) console.log(xhr.responseText)
}
xhr.send()
開班時間:2021-04-12(深圳)
開班盛況開班時間:2021-05-17(北京)
開班盛況開班時間:2021-03-22(杭州)
開班盛況開班時間:2021-04-26(北京)
開班盛況開班時間:2021-05-10(北京)
開班盛況開班時間:2021-02-22(北京)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2020-09-21(上海)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2019-07-22(北京)
開班盛況
Copyright 2011-2023 北京千鋒互聯科技有限公司 .All Right
京ICP備12003911號-5
京公網安備 11010802035720號