Reactjs 设置axios的baseURL以用于条带/React

Reactjs 设置axios的baseURL以用于条带/React,reactjs,axios,stripe-payments,Reactjs,Axios,Stripe Payments,我目前正在尝试使用Stripe和react处理付款。然而,在本教程之后,我得到了一个错误404,没有找到我的api/支付意图 const stripe = new Stripe('here is my key'); export default async (req, res) => { if (req.method === "POST") { try { const { amount } = req.body; // Psst.

我目前正在尝试使用Stripe和react处理付款。然而,在本教程之后,我得到了一个错误404,没有找到我的api/支付意图


const stripe = new Stripe('here is my key');

export default async (req, res) => {
  if (req.method === "POST") {
    try {
      const { amount } = req.body;
      // Psst. For production-ready applications we recommend not using the
      // amount directly from the client without verifying it first. This is to
      // prevent bad actors from changing the total amount on the client before
      // it gets sent to the server. A good approach is to send the quantity of
      // a uniquely identifiable product and calculate the total price server-side.
      // Then, you would only fulfill orders using the quantity you charged for.

      const paymentIntent = await stripe.paymentIntents.create({
        amount,
        currency: "usd"
      });

      res.status(200).send(paymentIntent.client_secret);
    } catch (err) {
      res.status(500).json({ statusCode: 500, message: err.message });
    }
  } else {
    res.setHeader("Allow", "POST");
    res.status(405).end("Method Not Allowed");
  }
};

这是axios柱

const { data: clientSecret } = await axios.post("../api/payment_intents", {
            amount: this.state.amount * 100
          });
我在另一篇文章中读到,我需要设置一个基本URL,但我不确定如何设置/这意味着什么。任何帮助都将不胜感激

更新:使用“localhost:3000/api/payment\u intents”,我能够得到关于CORS策略的不同错误消息


从源站访问“localhost:3000/api/payment\u intents”处的XMLHttpRequesthttp://localhost:3000'已被CORS策略阻止:跨源请求仅支持协议方案:http、data、chrome、chrome扩展、chrome untrusted、https。

如下定义baseURL:

let API = axios.create({
    baseURL: "http://base_url/api"
})

export default API;
然后在axios帖子中:

const { data: clientSecret } = await API.post("/payment_intents", {
    amount: this.state.amount * 100
});

通常情况下,您也希望将URL作为环境变量传入。

我仍然会收到一个错误,在使用“”时显示为not found(未找到)。我可以看看您是如何定义基本URL的吗?让API=axios.create({baseURL:“})在本地主机之前有一个http://但chrome显示为链接,API文件夹在我的根目录中,这需要不同吗?您需要包括http://... baseURL:“xhr.js:184发布404(未找到)