然后,Axios,catch不被调用

时间:2019-03-14 08:44:32

标签: python reactjs axios falconframework

在我的React前端中,我成功地使用post方法调用Axios,在我的Python Falcon后端参数中成功接收到了令牌,并且生成了令牌, 问题是永远不会调用.then甚至.catch中的代码,这是我的前端代码:

async submit() {
  //var aluser = this.this.state.username;
  await axios({
    method: "post",
    url: "http://127.0.0.1:8000/Login",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    params: {
      username: this.state.username,
      password: this.state.password
    }
  })
    .catch(error => {
      console.log(
        "here is the error on a post request from the python server  ",
        error
      );
    })
    .then(res => {
      console.log(res);

      sessionStorage.setItem("token", res.data[0]);
    });
}

注意:.then.catch的顺序之前已切换,结果相同。

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试使用try/catch

const params = new URLSearchParams();
params.append('username', this.state.username);
params.append('password', this.state.password);

async submit() {
  //var aluser = this.this.state.username;
  try {
    const res = await axios({
      method: "post",
      url: "http://127.0.0.1:8000/Login",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
      },
      params
    })

    sessionStorage.setItem("token", res.data[0]);
  } catch (err) {
    console.log(
      "here is the error on a post request from the python server  ",
      error
    );
  }
}