在 react-native 中使用 fetch 时如何获取我的响应状态?
How do i get the status of my response when using fetch in react-native?
我正在为我的 React Native 应用程序制作登录页面。当我的用户名和密码有效和无效时,我的 api 会发送不同的响应。所以我想跟踪并将我的响应状态保存在某个状态变量中,然后再相应地执行功能。请建议我这样做的方法。
doSignUp() {
console.log("inside post api");
fetch('MyApiUrl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
password: this.state.password,
email:this.state.email,
name: this.state.firstname,
last_name :this.state.last_name,
mobile:this.state.mobile,
ssn:"2222222"
})
}).then((response) => {console.log('response:',response.status);
response.json()}
.then((responseData) => {
console.log("inside responsejson");
console.log('response object:',responseData)
console.log('refresh token:',responseData[0].token.refresh_token)
console.log('access token:',responseData[0].token.access_token);
}).done();
}
据我了解,您想知道获取请求的 http 状态代码。
通常您的响应对象包含 "status" 属性。所以你应该能够通过使用这个来接收状态代码:
response.status
如果请求成功,这将是 return 200。
我正在为我的 React Native 应用程序制作登录页面。当我的用户名和密码有效和无效时,我的 api 会发送不同的响应。所以我想跟踪并将我的响应状态保存在某个状态变量中,然后再相应地执行功能。请建议我这样做的方法。
doSignUp() {
console.log("inside post api");
fetch('MyApiUrl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
password: this.state.password,
email:this.state.email,
name: this.state.firstname,
last_name :this.state.last_name,
mobile:this.state.mobile,
ssn:"2222222"
})
}).then((response) => {console.log('response:',response.status);
response.json()}
.then((responseData) => {
console.log("inside responsejson");
console.log('response object:',responseData)
console.log('refresh token:',responseData[0].token.refresh_token)
console.log('access token:',responseData[0].token.access_token);
}).done();
}
据我了解,您想知道获取请求的 http 状态代码。
通常您的响应对象包含 "status" 属性。所以你应该能够通过使用这个来接收状态代码:
response.status
如果请求成功,这将是 return 200。