【问题标题】:Firebase Cloud function via https.onCall通过 https.onCall 的 Firebase Cloud 功能
【发布时间】:2018-04-05 06:04:46
【问题描述】:

我正在尝试通过 https.onCall 从 Web 浏览器或使用 curl 命令调用 firebase 云功能。我通过 curl 得到以下响应:

{"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}

当我通过浏览器尝试(并在 firebase 上进行身份验证)时,我收到一个错误:域 ...“在 Access-Control-Allow-Origin 标头中找不到”,

这可能更多地与 http 跨域请求有关,而不是 firebase。我尝试将 Access-Control-Allow-Origin 标头设置为“*”,但无济于事。

由于我试图让 https.onCall 正常工作,因此我为服务器功能使用了一种超级简单的方法:

        exports.testClientCall = functions.https.onCall((req, res) => { 
         var jsonObj = {"Founder": "Apple"};
        var json = JSON.stringify(jsonObj);
      response.send(json);
        });

网页浏览器中的客户端功能是:

    function getDataFromFirebaseFunction() {
    var http = new XMLHttpRequest();
    var url = "https://us-central1-<myproject>.cloudfunctions.net/testClientCall";
    var params = {"hello":"friend" };
    http.open("POST", url, true);
    
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/json");
    http.setRequestHeader("X-MyHeader", "123");
    http.setRequestHeader("Access-Control-Allow-Origin", "*");
    http.setRequestHeader("Access-Control-Allow-Methods", "POST");
     
    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);
    }

全面努力通过客户端执行firebase云功能。有什么想法吗?

【问题讨论】:

  • 您为什么不使用客户端 SDK,它负责管理客户端和服务器之间的协议的所有繁重工作? firebase.google.com/docs/functions/callable
  • 对不起,你是对的。会试一试的。
  • 谢谢。问题解决了。但是,我需要向 firebase 函数 src 添加一个“延迟”,如下所示:
  • @RowanGontier 我可以请您发布您的解决方案作为答案吗?
  • 已完成 - 答案中的解决方案。

标签: firebase google-cloud-functions


【解决方案1】:
 <!DOCTYPE html>
        <html>
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
        
          <!-- update the version number as needed -->
          <script defer src="/__/firebase/4.12.1/firebase-app.js"></script>
          <!-- include only the Firebase features as you need -->
          <script defer src="/__/firebase/4.12.1/firebase-auth.js"></script>
          <script defer src="/__/firebase/4.12.1/firebase-database.js"></script>
          <script defer src="/__/firebase/4.12.1/firebase-messaging.js"></script>
          <script defer src="/__/firebase/4.12.1/firebase-storage.js"></script>
          <script defer src="https://www.gstatic.com/firebasejs/4.12.1/firebase-functions.js"></script>
        
          <!-- initialize the SDK after all desired features are loaded -->
          <script defer src="/__/firebase/init.js"></script>   
        
          <script>
          function executeHttpsCallableUsingClientSDK() {
    //Send a name to firebase cloud function, and get response data
    
            var testClientCall = firebase.functions().httpsCallable('testClientCall');
            testClientCall ({name: "Steve Jobs"}).then(function(result) {
          // Read result of the Cloud Function.
              console.log("Client done with firebase function request, with result:");
              console.log(result);
        });
          }
          </script>
        </head>
        <body>
        
        </body>
        </html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 2021-09-14
    • 2018-12-06
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多