无法读取react上的响应对象



我的django api返回用户名,如果用户通过了身份验证,则返回未通过身份验证的详细信息但是我无法读取状态码或控制台日志或捕获401状态码,res.status给出未定义的MSG控制台img

如何使用http状态来渲染取决于收到的代码

export default class GetUser extends Component {.....}
componentDidMount(){
fetch("http://127.0.0.1:8000/app/ebooks/",
{ CSRF_TOKEN.....
},
})
.then(res => res.json())
.then(res => { 
this.setState({data:[res]})
console.log(resp.status) --->UNDIFINED
})
.catch(err => console.log(err));
}

render() {
var x = this.state.data
return (
<pre>{JSON.stringify(x, null, 2) }</pre>
)
}
}

将console.log()移到第一个then子句。

.then(response => {
console.log(response.status) --> 401
return response.json()
})
.then(data => { 
this.setState({data:[data]})
})
.catch(err => console.log(err));

最新更新