【问题标题】:How to store data with a deployed app and deployed functions on firebase如何在 Firebase 上使用已部署的应用程序和已部署的函数存储数据
【发布时间】:2018-12-25 15:40:34
【问题描述】:

我有这个角码来获取数据

$http({
    header: 'content-type:application/json',
    method:'GET',
    url: 'https://us-central1-****-*****.cloudfunctions.net/**/GetAll****',
  }).then(function(messages){
    $scope.member = messages.data.****
  })

控制台中的错误是

Failed to load https://us-central1-site-*****.cloudfunctions.net/**/GetAll****: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://site-******.firebaseapp.com' is therefore not allowed access.

谁能帮帮我

【问题讨论】:

    标签: node.js angular firebase


    【解决方案1】:

    只需在您的云函数中发送响应之前添加这些行,因为您需要启用 CORS

    function foo(req, res) {  
      res.set('Access-Control-Allow-Origin', "*");
      res.set('Access-Control-Allow-Methods', 'GET, POST');
      res.set('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token'); // <-- Try adding this line
      res.status(200).send('custom message');
    }
    

    【讨论】:

    • 你的意思是“res”的快递应用名称?
    • 是的,res 我的意思是快速应用程序中的 res 对象。我已经更新了我的答案,以提供有关在何处使用标题的更多上下文。
    • 在 POST 请求中出现此错误:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头
    • 我添加了第三个标题,您可以添加它再试一次吗?
    • 同样的问题在控制台中没有任何改变
    【解决方案2】:

    我遇到了同样的问题,但是当我尝试时。通常会发生此错误,因为当您尝试访问“http”到“https”时,浏览器会出于安全原因进行阻止。你需要做的是,像下面这样使用cors

    第 1 步:首先在 firebase 函数中安装带有 npm 的 cors。

    第 2 步:安装 cors 后,按以下方式编写函数

    const cors = require('cors')({ origin: true }); // origin true must be there
    exports.sampleFunction = functions.https.onRequest((request, response)=>{
       cors(request,response, () =>{
           // Do your business logic or code.
       })
    })
    

    【讨论】:

    • 我的回答对您有帮助吗?如果是,请考虑支持答案
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    相关资源
    最近更新 更多