首页 > 解决方案 > 如何从 expressjs 发送数据以响应客户端?

问题描述

我有一个payments.js页面使用自定义表单来制作带有条纹的付款,看起来像这样

async handleSubmit() {
    const { firstName, lastName, email, cardNo, expiryDate, cvv, nameOnCard } = this.state
    const { price } = this.props.location.state.item

    this.setState({
        loading: true
    });

    await axios.post("/charge", {
        firstName,
        lastName,
        email,
        cardNo,
        expiryDate,
        cvv,
        nameOnCard,
        price
    }).then((res) => this.setState({ iframeLink: res.data.next_action.redirect_to_url.url, loading: false }))
        .catch((error) => console.log('ERR', error))

    // I want this to wait for changes
    await axios.post("/api/stripe/webhooks").then((res) => console.log(res)).catch((err) => console.log(err))

}

我正在打开在第一篇文章中收到的 url iframe,一旦 3d 安全完成,我想用这条线获得条纹钩子,在第一次发布后调用一次,然后一旦收到另一个钩子就不会更新

await axios.post("/api/stripe/webhooks").then((res) => console.log(res)).catch((err) => console.log(err))

我的问题是此操作中从未收到数据

在服务器中,post 函数看起来像这样

// Stripe Webhooks
app.post("/api/stripe/webhooks", async function (req, res) {
  try {
    const query = req.body;
    res.sendStatus(200);
    console.log(query)
    res.end(JSON.stringify(query))
  } catch (err) {
    console.log("/webhooks route error: ", err);
  }
});

知道如何在收到数据时捕获数据吗?

标签: reactjsexpressstripe-payments

解决方案


推荐阅读