gpt4 book ai didi

python - axios 然后,catch 没有被调用

转载 作者:行者123 更新时间:2023-12-01 08:12:26 26 4
gpt4 key购买 nike

在我的React前端中,我使用post方法成功调用Axios,在我的Python Falcon后端中成功接收参数并生成 token ,问题是 .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的顺序,结果是一样的。

提前致谢

最佳答案

尝试使用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
);
}
}

关于python - axios 然后,catch 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55158185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com