【发布时间】:2020-10-30 17:36:36
【问题描述】:
发送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)
})
【问题讨论】:
-
console.log(this)你会发现问题。 -
是的,这都是关于“this”所指的我使用了 function(){} 语法并且它有效
标签: javascript http xmlhttprequest