简体   繁体   English

从反应到表达的错误 404 POST 表单数据

[英]Error 404 POST form data from react to express

I am trying to post data from a form (react) to my backend (nodejs/express), but I am getting a 404 error.我正在尝试将表单(react)中的数据发布到我的后端(nodejs/express),但出现 404 错误。 There is a lot of code here, so I've tried to post just what is important.这里有很多代码,所以我试图发布重要的内容。

Function to submit Function 提交

  //submit data
  const submitCard = (ev) => {
    console.log(ev);
    ev.preventDefault();

    try {
      api.post('/api/stripe/', payment).then((result) => {
        window.location.href = process.env.REACT_APP_REDIRECT_URI;
      });
    } catch (e) {
      console.log(e);
    }
  };

data being submitted正在提交的数据

  const paymentUpdated = () => {
    setPayment({
      name: name,
      firstName: firstName,
      lastName: lastName,
      cardNumber: number,
      expDate: expiry,
      cvc: cvc,
      zip: zip,
      plan: plan,
    });
  };

form tag + button表单标签+按钮

<form className="creditForm" onSubmit={submitCard}>
<Button className="save-button" type="submit">

route being submitted to (still need send data to stripe)提交到的路由(仍然需要将数据发送到条带)

 app.post('/api/stripe', requireLogin, async (req, res) => {
    const plan = await stripe.plans.retrieve('price_1HfAZGGFN31Q4RRjSrFXnGgD');
    console.log(req);
    if (req.user.stripeId) {
      const customer = await stripe.customers.retrieve(req.user.stripeId);
    } else {
      const customer = await stripe.customers.create({
        email: req.user.email,
        description: 'Subscribing to Personal Plan Monthly Subscription',
      });
      req.user.stripeId = customer.id;
      const user = await req.user.save();
    }
    req.user.plan = 2;
    const user = await req.user.save();
    const subscription = await stripe.subscriptions.create({
      customer: req.user.stripeId,
      items: [{ price: 'price_1HfAZGGFN31Q4RRjSrFXnGgD' }],
    });

It looks like there is an inconsistency in the URL your posting to and that configured in express .看起来您发布到的 URL 与express中配置的不一致。 Your posting to /api/stripe/ , but express is setup to listen for /api/stripe , note the lack of trailing / in express.您发布到/api/stripe/ ,但 express 设置为监听/api/stripe请注意缺少尾随/在 express 中。 So it is not finding the endpoint you are expecting.所以它没有找到您期望的端点。

I tend to test my API s using curl commands eg我倾向于使用curl命令测试我的API ,例如

curl -X POST "http://localhost:3500/api/stripe/" -H  "content-type: application/json" -d '{"key1": "value1", "key2": "value 2"}'

Once I have proven the API works, only then do I worry about calling it from an app.一旦我证明 API 有效,我才担心从应用程序调用它。 This will make it easier to identify where the problem lies, in your code.这将使您更容易识别代码中的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM