错误:在带有 axios 的 react-native 中请求失败,状态码为 400

时间:2021-02-08 07:05:53

标签: react-native

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。我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

data: qs.stringify({
"Username": "admin@diginepal.com",
"Password": "password@123456",
"grant_type": "password"
})


headers: { 
        'Content-Type': 'application/x-www-form-urlencoded', 
        },

使用 qs 并更改标题解决了我的问题。

答案 1 :(得分:0)

很高兴您设法解决了问题,以备将来参考,如果您遇到 400 Bad Request 错误,我发现打印 error.response.data 以获得更有意义的错误消息非常有用。

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