Javascript 无法加载XMLHttpRequesthttps://localhost:8585/pay 由于访问控制检查

Javascript 无法加载XMLHttpRequesthttps://localhost:8585/pay 由于访问控制检查,javascript,node.js,reactjs,express,Javascript,Node.js,Reactjs,Express,我在应用程序的后端使用nodeJS和expressJS,前端使用ReactJS。在我的计算机上本地运行时,一切正常。但是,我最近尝试使用Firebase部署该应用程序。应用程序在firebase的给定链接上成功启动。 事情发生了 它只在我的桌面浏览器上工作。如果我试图在其他设备上运行我的网站,它就不起作用。首先,在桌面上,它在Chrome中工作。在狩猎中,不工作。在控制台中,它显示“XMLHttpRequest无法加载”http://localhost:8585/pay 由于访问控制检查。” 我

我在应用程序的后端使用nodeJS和expressJS,前端使用ReactJS。在我的计算机上本地运行时,一切正常。但是,我最近尝试使用Firebase部署该应用程序。应用程序在firebase的给定链接上成功启动。 事情发生了

  • 它只在我的桌面浏览器上工作。如果我试图在其他设备上运行我的网站,它就不起作用。首先,在桌面上,它在Chrome中工作。在狩猎中,不工作。在控制台中,它显示“XMLHttpRequest无法加载”http://localhost:8585/pay 由于访问控制检查。” 我搜索它。我制作了一个https本地主机来解决这个问题。现在在我的桌面上,chrome和safari都可以工作了!!!不幸的是,当我在其他设备上运行该站点时。这根本不管用
  • 然而,当我在手机上查看我的网站时,chrome和safari不适用于我的网站。我检查了它给我的控制台
  • 我已经找了好几天了。我不知道为什么它只能在我的桌面上工作,而不能在其他设备上工作。 我有app.use(cors());修复访问控制允许来源问题。我不知道发生了什么事

    这是我的index.js

    const functions = require('firebase-functions');
    
    const express = require('express')
    const app = express()
    const cors = require('cors')
    const bodyParser = require('body-parser')
    const stripe = require('stripe')('sk_test_51HJ0tkCo200wpjbfBGB');
    var path = require('path')
    var fs = require('fs')
    var https = require('https')
    var certOptions = {
      key: fs.readFileSync(path.resolve('./server.key')),
      cert: fs.readFileSync(path.resolve('./server.crt'))
    }
    
    const port = 8585
    app.use(cors());
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))
    
    
    // parse application/json
    app.use(bodyParser.json())
    
    app.get('*', (req, res) => {
      res.send("Hello from the API")
    })
    
    app.post('/pay', async (req, res) => {
        const {amount, billingDetails} = req.body;
        const customer = await stripe.customers.create({
          email: billingDetails.email,
          name: billingDetails.name,
          phone: 333333,
         
        });
    
        const paymentIntent = await stripe.paymentIntents.create({
            amount: amount,
            currency: 'usd',
            customer: customer.id,
            // Verify your integration in this guide by including this parameter
            metadata: {integration_check: 'accept_a_payment'},
            receipt_email: billingDetails.email,
            description: `Purchased the ${billingDetails.name}`,
            shipping: {
              name: billingDetails.name,
              address: {
                line1: billingDetails.address.line1,
                city: billingDetails.address.city,
                country: billingDetails.address.country,
                postal_code: billingDetails.address.postal_code
              }
            }
          });
    
          res.json({'client_secret': paymentIntent['client_secret']})
    })
    
    
    https.createServer(certOptions, app).listen(port, () => console.log(`Example app listening on port ${port}! https://localhost:${port}`))
    
    //app.listen(port, () => console.log(`Example app listening on port ${port}! https://localhost:${port}`))
    exports.api = functions.https.onRequest(app)
    
    在我的回复中,我发了一篇帖子

    const res = await axios.post('https://localhost:8585/pay', {
            amount: (props.totalPrice*100).toFixed(0), 
            billingDetails: billingDetails,
            });
    
    在我的firebase.json中

    {
      "hosting": {
        "public": "build",
        "ignore": [
          "firebase.json",
          "**/.*",
          "**/node_modules/**"
        ],
        "rewrites": [
          {
            "source": "/",
            "destination": "/index.html"
          },
          {
            "source": "/api",
            "function": "api"
          }
        ]
      }
    }
    
    我已经试着像这样提出发帖请求了

    const res = await axios.post('/pay'
    

    它给了我

    the server responded with a status of 404 ()
    
    因此,它不起作用。请帮助我。

    您必须将本地主机更改为您的firebase ip

    const res = await axios.post('/api/pay'
    
    the server responded with a status of 404 ()