我如何在axios发布请求中添加json正文?

时间:2020-07-14 09:39:31

标签: reactjs api axios

  submitForm = (e) => {
    e.preventDefault();
    sessionStorage.setItem('username', this.state.username);
    sessionStorage.setItem('password', this.state.password);

    axios.post('/authenticate', {data: {
      username: sessionStorage.getItem('username'),
      password: sessionStorage.getItem('password')
    }})
    .then((response) => {
       this.setState({
         jwttoken: response.data
       })
     },
     sessionStorage.setItem('token', this.state.jwttoken)
     )

这是我的发帖请求,但似乎不起作用 我需要在正文中添加用户名和密码, 我做对了吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试;

const url = Your url
const { username, password } = this.state
const data = {
    username,
    password
}
axios.post(url, data)
...