【问题标题】:Cors - Access-Control-Allow-Origin Error in firebase cloud functionsCors - Firebase 云功能中的访问控制允许来源错误
【发布时间】:2020-08-18 18:25:42
【问题描述】:

这是我得到的错误:

Access to XMLHttpRequest at has been blocked by CORS policy: Response to preflight request 
doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我的云功能:

...
app.use(cors({
  origin: true,
}))
app.use(express.json());
app.use(express.urlencoded({extended: false}));


app.post('/submit', async (req, clientRes) => {
...
}

exports.app = functions.region('europe-west2').runWith({timeoutSeconds: 540, memory: '2GB'}).https.onRequest(app);

这是我从客户端发出的发布请求:

axios.post('/submit', body, {headers: {
      // 'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    }});

我尝试设置Access-Control-Allow-Origin: '*'Access-Control-Allow-Origin: mydomain.comAccess-Control-Allow-Origin: cloudFunctionDomain.net,但仍然无法正常工作。

非常感谢您的帮助。

【问题讨论】:

    标签: node.js firebase axios cors google-cloud-functions


    【解决方案1】:

    根据 [1],这种导入方式似乎对许多人都有效:

    const cors = require('cors')({origin: true});
    

    如 [1] 中所述,您还可以从此处 [2] 查看代码示例。

    [1]Enabling CORS in Cloud Functions for Firebase

    [2]https://github.com/firebase/functions-samples/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2017-12-30
      • 1970-01-01
      • 2021-12-03
      • 2017-12-28
      • 2019-02-09
      • 2016-06-02
      • 2013-03-02
      相关资源
      最近更新 更多