如果用户已通过身份验证,我的django api将使用用户名进行响应,如果未通过身份验证,则会返回一个未通过身份验证的详细信息msg,但我无法读取状态代码或控制台日志,也无法捕获401状态代码,而response.status给出了未定义的msg
控制台img
如何使用http状态根据收到的代码进行呈现。
export default class GetUser extends Component {
constructor(props) {
super(props);
this.state={
data : [],}
}
componentDidMount(){
fetch("http://127.0.0.1:8000/app/ebooks/",
{
credentials: 'include',
method: 'GET',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRFToken': CSRF_TOKEN
},
})
.then(response => response.json())
.then(response => {
this.setState({data:[response]})
console.log(response.status) --->UNDIFINED
})
.catch(err => console.log(err));
}
render() {
var x = this.state.data
return (
{JSON.stringify(x, null, 2) }
)
}
}
发布于 2021-02-28 11:03:14
将console.log()上移到第一个then子句。
.then(response => {
console.log(response.status) --> 401
return response.json()
})
.then(data => {
this.setState({data:[data]})
})
.catch(err => console.log(err));
https://stackoverflow.com/questions/66408168
复制