【问题标题】:Firebase Cloud Functions: Cors examples don't workFirebase Cloud Functions:Cors 示例不起作用
【发布时间】:2019-11-08 08:33:00
【问题描述】:

我正在尝试从我的本地 Web 应用程序调用一个简单的 helloWorld firebase 云函数。当我调用该函数时,Firebase 中的日志返回状态代码 200,但我遇到了 CORS 问题。我已经实现了CORS solution steps suggested by Firebase(参见下面的 index.js 代码),但这在我的情况下不起作用。还有什么我可以尝试解决这个问题的吗?

当我在浏览器中调用端点时,它返回:

 {"headers":{"Access-Control-Allow-Origin":"*"},"body":{"message":"Hello world"}}

但是当我从我的应用中获取时:

 fetch(endpoint, {
   method: "GET", 
   mode: "no-cors", 
   cache: "no-cache", 
   credentials: "same-origin", 
   headers: {
     "Content-Type": "application/json; charset=utf-8",
   },
   redirect: "follow", 
   referrer: "no-referrer", 
 }).then(response => {
   console.log("response", response)
 });

我得到一个不透明的响应:

我的 index.js 文件:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();

app.use(cors);
app.get('/', (req, res) => {
  res.set('Access-Control-Allow-Origin', '*')
  res.status(200).send({
    headers: {'Access-Control-Allow-Origin': '*'},
    body: { "message": "Hello world"},
  });
});

exports.helloWorld = functions.https.onRequest(app);

【问题讨论】:

    标签: firebase cors google-cloud-functions


    【解决方案1】:

    在提取时使用mode: cors 而不是mode: no-cors

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2012-07-05
      • 2013-12-04
      • 2016-08-31
      • 2016-02-25
      相关资源
      最近更新 更多