我正在尝试从我的React Native项目中通过oauth / tokens登录。我用POSTMAN测试当我从axios发送数据时它运行良好。但是,当我尝试从我的应用程序发送它时,它给了我错误:请求失败,状态代码为400 我的代码是:
axios({
method: 'post',
url: 'http://183.172.100.84:8000/oauth/token',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
data: {
client_id : '2',
client_secret : 'secret',
grant_type : 'password',
email : this.state.userEmail,
password : this.state.userPassword
}
})
.then((response) => {
console.log(response.data);
})
答案 0 :(得分:0)
我已经用这种方式解决了这个问题:
axios({
method: 'post',
url: 'http://183.172.100.84:8000/oauth/token',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
data: 'client_id=2&client_secret=secret&grant_type=password&email=' + this.state.userEmail+'&password='+this.state.userPassword
})
.then((response) => {
console.log(response.data);
})
似乎Axios在以json格式处理数据方面存在一些问题。
如果您使用Webpack,还必须配置代理。