首页 > 解决方案 > 错误:在带有 axios 的 react-native 中请求失败,状态码为 400

问题描述

axios({
method: 'POST',
url: `http://${base_url}/token`,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
"Username": "admin@diginepal.com",
"Password": "password@123456",
"grant_type": "password"
}
})
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})

我收到错误:请求失败,状态码为 400。我在这里做错了什么?

标签: react-native

解决方案


很好,您设法解决了,以供将来参考,如果您有 400 Bad Request 错误,我发现打印 error.response.data 以获取更有意义的错误消息非常有用。

axios.post('/').then(response => {
     console.log(response.data);
 }).catch(error => {
     console.log(error.response.data);
 });

推荐阅读