【发布时间】:2022-01-22 07:04:15
【问题描述】:
关于 JS 中的承诺解决,我有以下问题。
前端代码:
const test = () => {
try {
let response = axios.post("http://localhost:5000/auth/test").then(
(res) => {
console.log("received")
console.log(res)
});
console.log(response);
} catch (error) {
console.error(error);
}
};
后端python代码:
@auth.route("/test", methods=["POST"])
def test():
import time
time.sleep(0.5)
return jsonify("Test Request"), 200
在我的控制台中显示*Promise {<pending>}*,但从未“收到”。这是为什么?如何等待后端的响应?
【问题讨论】:
-
您是否测试了后端路由本身?它适用于卷曲吗?顺便说一句,你能试试 async/await 吗?
-
嗨,是的,我现在还添加了“GET”作为方法:❯ curl -X GET "localhost:5000/auth/test" 返回 {"msg":"Test Request"}
-
好的。尝试使用 axios.get().then().catch(); (没有尝试捕获)。或者尝试使用 async/await。
-
这似乎也不起作用:( ``` const test = async () => { await axios.get("localhost:5000/auth/test").then( (res) => { console.log("收到") console.log(res) }).catch(err => { console.error(err); }); }; ``
-
Http 方法应该是
POST而不是GET。
标签: javascript axios es6-promise