axios.post在setTimeout周围没有返回400

编程入门 行业动态 更新时间:2024-10-07 19:23:20

axios.<a href=https://www.elefans.com/category/jswz/34/1771420.html style=post在setTimeout周围没有返回400"/>

axios.post在setTimeout周围没有返回400

app.post('/orders', (req, res) => {
    console.log(req.body) 
    // more code

    setTimeout(function () {
         axios.post('', payload, { headers: headers })
            .then(function (response) {
                if (response.status === 201) {
                    res.sendStatus(201);                    
                }
            })
            .catch(function (error) {
                res.status(500).send({ error: error });
            });
    }, 500);    
});

我在从test端点获取400响应时遇到了麻烦,我发现当我用axios.post()包裹setTimeout时,最终给了我201响应。为什么是这样?我怎样才能更好地改善它?

回答如下:

根据我的经验,您无法直接从外部将变量传递到setTimeout函数中。您可以尝试创建全局变量并在setTimeout函数中访问它,也可以尝试使用getFunction来访问外部变量。

更多推荐

axios.post在setTimeout周围没有返回400

本文发布于:2024-05-07 10:59:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:post   axios   setTimeout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!