由于CORS问题,firebase托管阻止脚本 [英] firebase hosting blocking script due to CORS issue

查看:81
本文介绍了由于CORS问题,firebase托管阻止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase托管来托管一些脚本,并尝试从另一个站点访问它们。由于CORS问题,它自然会被阻止。根据我对其他论坛线程等的研究,我如下修改了firebase.json

I am using firebase hosting to host few scripts and trying to access them from another site. it naturally gets blocked due to CORS issues. based on my research on other forum threads etc i modified the firebase.json as below

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "headers": [ {
    "source" : "**",
    "headers" : [ {
      "key" : "Access-Control-Allow-Origin",
      "value" : "*"
    } ]
  }]
}
}

实际上,它允许任何URL访问此处托管的资源。但是,在尝试运行我的网站时,我仍然看到

which essentially allow any url to access the resources hosted here. however, on trying to run my site i still see below

        Access to XMLHttpRequest at 'https://oracle-bot-sdk.firebaseapp.com//loader.json' 
    from origin 'https://insurance-bot.moblize.it' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

还需要什么?

推荐答案

除了您对cors的firebase.json更改之外,您的firebase函数http / https函数还需要包含cors插件。

In addition to your firebase.json changes for cors, your firebase functions http / https function needs to also include the cors plugin.

示例

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

const app = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        // Your app stuff here

        // Send Response
        res.status(200).send(<response data>);
    });
});

Express App Example

import express from "express";
const cors = require('cors')({origin: true});

const app = express();
app.get('**', (req, res) => {
  cors(req, res, () => {
      // Your App Here

      // Send response
      res.status(200).send(<response data>);
    });
});

更多文档通过云函数提供动态内容-为托管站点创建HTTP函数文档btw 中未提及Cors)

More documentation Serve Dynamic Content with Cloud Functions - Create an HTTP function to your Hosting site (Cors is not mentioned in the documentation btw)

这篇关于由于CORS问题,firebase托管阻止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆