Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为Firebase使用云函数时出现CORS错误_Node.js_Api_Axios_Google Cloud Functions_Cors - Fatal编程技术网

Node.js 为Firebase使用云函数时出现CORS错误

Node.js 为Firebase使用云函数时出现CORS错误,node.js,api,axios,google-cloud-functions,cors,Node.js,Api,Axios,Google Cloud Functions,Cors,这是我提出的第一个问题,如果格式不好,我很抱歉 我一直在试图找出如何处理以下CORS错误,以及CORS飞行前错误: …已被CORS策略阻止:“访问控制允许来源”标头具有值https://example.com/'这不等于提供的原点 在过去的几天里,我阅读了关于这个主题的每一个问题,以及在CORS/Cloud Functions/Axios/等网站上可以找到的所有其他文档。我正在使用React、Node、Express、Axios、Google Firebase托管和Google Cloud功能

这是我提出的第一个问题,如果格式不好,我很抱歉

我一直在试图找出如何处理以下CORS错误,以及CORS飞行前错误:

…已被CORS策略阻止:“访问控制允许来源”标头具有值https://example.com/'这不等于提供的原点

在过去的几天里,我阅读了关于这个主题的每一个问题,以及在CORS/Cloud Functions/Axios/等网站上可以找到的所有其他文档。我正在使用React、Node、Express、Axios、Google Firebase托管和Google Cloud功能

我正在尝试访问PayPal API,以获得一个承载令牌,以进行进一步的API请求。我知道一些与请求头相关的代码可能是多余的。我只是想对这件事说点什么

有人有什么想法吗

节点文件-index.js

const axios = require("axios");
const express = require("express");
const cors = require("cors")({ origin: true });

const app = express();

app.use(cors());
app.use(express.json());

app.post("/v1/oauth2/token/", cors(), (req, res) => {
  res.set("Access-Control-Allow-Origin", "https://example.com/");
  var data = qs.stringify({
    grant_type: "client_credentials",
  });

  var config = {
    method: "post",
    url: "https://api-m.sandbox.paypal.com/v1/oauth2/token/",
    headers: {
      "Access-Control-Allow-Origin": "https://example.com/",
      Authorization:"xyz",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: data,
  };

  axios(config)
    .then(function (response) {
      let bearerToken = response.data.access_token;
      res.status(201).send(bearerToken);
    })
    .catch(function (error) {
      console.log(error);
    });
});

exports.api = functions.https.onRequest(app);
import axios from "../axios/axios";

 const oneTimePaypalPayment = async () => {
    const response = await axios.post("/v2/checkout/orders");
    console.log(response);
  };
import axios from "axios";

const instance = axios.create({
  headers: {
    "Access-Control-Allow-Origin": "https://example.com/",
    "Access-Control-Allow-Headers": "https://example.com/",
  },
  baseURL: "https://us-central1-example.cloudfunctions.net/api/"
});

export default instance;
react文件-payment.js

const axios = require("axios");
const express = require("express");
const cors = require("cors")({ origin: true });

const app = express();

app.use(cors());
app.use(express.json());

app.post("/v1/oauth2/token/", cors(), (req, res) => {
  res.set("Access-Control-Allow-Origin", "https://example.com/");
  var data = qs.stringify({
    grant_type: "client_credentials",
  });

  var config = {
    method: "post",
    url: "https://api-m.sandbox.paypal.com/v1/oauth2/token/",
    headers: {
      "Access-Control-Allow-Origin": "https://example.com/",
      Authorization:"xyz",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: data,
  };

  axios(config)
    .then(function (response) {
      let bearerToken = response.data.access_token;
      res.status(201).send(bearerToken);
    })
    .catch(function (error) {
      console.log(error);
    });
});

exports.api = functions.https.onRequest(app);
import axios from "../axios/axios";

 const oneTimePaypalPayment = async () => {
    const response = await axios.post("/v2/checkout/orders");
    console.log(response);
  };
import axios from "axios";

const instance = axios.create({
  headers: {
    "Access-Control-Allow-Origin": "https://example.com/",
    "Access-Control-Allow-Headers": "https://example.com/",
  },
  baseURL: "https://us-central1-example.cloudfunctions.net/api/"
});

export default instance;
axios文件-axios.js

const axios = require("axios");
const express = require("express");
const cors = require("cors")({ origin: true });

const app = express();

app.use(cors());
app.use(express.json());

app.post("/v1/oauth2/token/", cors(), (req, res) => {
  res.set("Access-Control-Allow-Origin", "https://example.com/");
  var data = qs.stringify({
    grant_type: "client_credentials",
  });

  var config = {
    method: "post",
    url: "https://api-m.sandbox.paypal.com/v1/oauth2/token/",
    headers: {
      "Access-Control-Allow-Origin": "https://example.com/",
      Authorization:"xyz",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: data,
  };

  axios(config)
    .then(function (response) {
      let bearerToken = response.data.access_token;
      res.status(201).send(bearerToken);
    })
    .catch(function (error) {
      console.log(error);
    });
});

exports.api = functions.https.onRequest(app);
import axios from "../axios/axios";

 const oneTimePaypalPayment = async () => {
    const response = await axios.post("/v2/checkout/orders");
    console.log(response);
  };
import axios from "axios";

const instance = axios.create({
  headers: {
    "Access-Control-Allow-Origin": "https://example.com/",
    "Access-Control-Allow-Headers": "https://example.com/",
  },
  baseURL: "https://us-central1-example.cloudfunctions.net/api/"
});

export default instance;
我尝试过的

我尝试使用通配符“*”只是为了让它工作,但没有运气。我在另一个答案上读到,谷歌云功能无论如何都无法识别“*”。我还尝试了下面的所有代码,以及在请求头上操纵访问控制Allow Origin的许多其他方法

const allowCrossDomain = function (req, res, next) {
    res.header("Access-Control-Allow-Headers", "*");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
  
    next();
  };

app.use(allowCrossDomain);

app.all("*", (req, res, next) => {
    res.header("Access-Control-Allow-Headers", "*");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
    next();
  });

  const corsOptions = {
    origin: "https://example.com/",
  };

app.use(cors(corsOptions))

app.options("/v1/oauth2/token", cors(corsOptions));

app.use(cors({origin:true}));

对于您正在导出或使用以下源代码的云功能,如果您有任何想法,我们将不胜感激

exports.your function = async (req, res) => {
  res.set('Access-Control-Allow-Origin', '*');

使用
原点:https://example.com“
-注释不带尾随斜杠!我刚试过,但不幸的是它不起作用。我将继续使用原点和尾随斜杠,如果发生任何事情,我会通知您。