簡體   English   中英

通過 https.onCall 的 Firebase Cloud 功能

[英]Firebase Cloud function via https.onCall

我正在嘗試通過 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);
        });

Web 瀏覽器中的客戶端功能是:

    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 雲功能。 有什么想法嗎?

 <!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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM