为什么if语句不检查下面http请求的状态?
Why does not if statement check the status of the http request below?
发送 http 请求后,我检查状态是否为 200 然后 resolve()
但 if 语句似乎不起作用
function getMelumat(){
let promise = new Promise((resolve,reject)=>{
let xhr = new XMLHttpRequest();
xhr.open("GET",'https://jsonplaceholder.typicode.com/posts');
xhr.send()
xhr.onload = ()=>{
if ( this.status == 200){
resolve(this.response)
}
else{
throw "AN ERROR OCCURED"
}
}
})
return promise
}
getMelumat().then(data=>{
return JSON.parse(data)
}).then(data=>{
console.log(data)
})
检查状态是否为 200 是不够的;您还需要检查 this.readyState
。
headers(带有状态)将在 this.readyState == 2
可用,但整个响应要到 this.readyState == 4
。
才能完成
发送 http 请求后,我检查状态是否为 200 然后 resolve() 但 if 语句似乎不起作用
function getMelumat(){
let promise = new Promise((resolve,reject)=>{
let xhr = new XMLHttpRequest();
xhr.open("GET",'https://jsonplaceholder.typicode.com/posts');
xhr.send()
xhr.onload = ()=>{
if ( this.status == 200){
resolve(this.response)
}
else{
throw "AN ERROR OCCURED"
}
}
})
return promise
}
getMelumat().then(data=>{
return JSON.parse(data)
}).then(data=>{
console.log(data)
})
检查状态是否为 200 是不够的;您还需要检查 this.readyState
。
headers(带有状态)将在 this.readyState == 2
可用,但整个响应要到 this.readyState == 4
。