簡體   English   中英

從反應到表達的錯誤 404 POST 表單數據

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

我正在嘗試將表單(react)中的數據發布到我的后端(nodejs/express),但出現 404 錯誤。 這里有很多代碼,所以我試圖發布重要的內容。

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);
    }
  };

正在提交的數據

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

表單標簽+按鈕

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

提交到的路由(仍然需要將數據發送到條帶)

 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' }],
    });

看起來您發布到的 URL 與express中配置的不一致。 您發布到/api/stripe/ ,但 express 設置為監聽/api/stripe請注意缺少尾隨/在 express 中。 所以它沒有找到您期望的端點。

我傾向於使用curl命令測試我的API ,例如

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

一旦我證明 API 有效,我才擔心從應用程序調用它。 這將使您更容易識別代碼中的問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM