我有以下代码,但没有控制台输出。看来,无论我做什么,都不会输入then()
或catch()
。
axios
.post(url, {test: "0"})
.then(console.log)
.catch(console.error);
足够有趣的是,请求被发布并在另一端被接收。我还可以通过与邮递员嘲笑来验证端点是否正确响应。哪里可能失败?
编辑:变得更加奇怪:我尝试了以下操作,并立即打印出承诺:
console.log(await axios.post(url, {test: "0"}));
不幸的是,我被迫使用Node v8.11.1-这可能是问题吗?
编辑2 :.then(console.log)
只是写.then(response => console.log(response))
的另一种方式,但是请确保我去过official doc并使用推荐的方式:
axios
.post(url, {test: "0"})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
问题仍然存在。
答案 0 :(得分:0)
根本不是Axios的问题。 POST发布后,Azure立即终止了我的进程,而没有等待Promise解决。如果有人遇到类似问题,请参见Promise is not working in azure function app javascript
答案 1 :(得分:-1)
当将console.log函数作为resolve回调传递时,您正在失去log函数的上下文。上下文是控制台
您将上下文重新绑定到功能日志
axios
.post(url, {test: "0"})
.then(console.log.bind(console))
.catch(console.error.bind(console));
axios承诺会工作。已绑定功能日志上下文:https://jsfiddle.net/dwk1nrpe/8/
您不会使用箭头功能丢失上下文
axios
.post(url, {test: "0"})
.then((res) => console.log(res))
.catch((err) => console.error(err));
希望它可以解决您的问题。