Axios发布请求在React Native中不起作用

时间:2019-12-05 11:55:00

标签: reactjs api react-native axios

我正在从API获取令牌,但是很不幸,我的API返回了400个错误的请求。我已经通过Postman检查了我的api,并且在那儿工作正常。请让我知道解决方法或任何错误。

async componentWillMount(){
 axios.post('http://api.myapiurl.com/token', {
                grant_type: 'PASSWORD',
                username: 'MY_USERNAME',
                password: 'MY_PASSWORD'
            }, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }).then(response => {
                console.log(response.data)
            }).catch(err => console.log("api Erorr: ", err.message))
}

捕获错误

  

请求失败,状态码为400

1 个答案:

答案 0 :(得分:2)

使用QueryString.stringify()自己解决。我只是将尸体传递到QueryString.stringify()中,如下所示:

axios.post('http://api.apiurl.com/token', QueryString.stringify({
            grant_type: 'MY_GRANT_TYPE',
            username: 'MY_USERNAME',
            password: 'MY_PASSWORD'
        }), {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            }
        }).then(response => {
            console.log(response.data)
        }).catch(err => console.log("api Erorr: ", err.response))
相关问题