簡體   English   中英

沒有 'Access-Control-Allow-Origin' - Firebase 函數

[英]No 'Access-Control-Allow-Origin' - Firebase Function

我使用 firebase 函數創建了一個 GET 請求。 我可以使用 Postman 接收數據,但不能從我的 FE 應用程序接收數據。

在訪問端點時,我收到此錯誤

from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我的FE代碼,

  pay() {
    this.http.get(this.settings.createPaymentRequest).subscribe(res => {
      console.log(res);
    })
  }

服務器代碼,

exports.createRequest = functions.https.onRequest((req, res) => {
const cors = require('cors')({
  origin: '*'
});
...
var options = {
    method: 'POST',
    uri: 'https://test.instamojo.com/api/1.1/payment-requests/',
    form: payload,
    headers: insta_headers,
    json: true
  };

  return rp(options)
    .then(function (response) {
      console.log(response);
      if (response.success) {

        db.collection('PAYMENT_REQUESTS').doc(response.payment_request.id).set({
          LONG_URL: response.payment_request.longurl,
          REQUEST_CREATED_AT: response.payment_request.created_at,
          PAYMENT_STATUS: "STARTED"
        }, {
          merge: true
        }).then(function () {
          response.setHeader(headers);
          res.status(200).send({
            STATUS: "SUCCESS",
            RESPONSE: response.payment_request.longurl
          });
        })
          .catch(function (error) {
            res.status(200).send({
              STATUS: "ERROR",
              RESPONSE: error
            });
          });



      } else return res.status(403).send('Forbidden!');

    })
    .catch(function (e) {
      console.log("ERROR" + e);
      res.status(200).send({
        STATUS: "ERROR",
        RESPONSE: e
      });
    });
});

請指導我如何解決這個問題。

看起來你創建了這個

const cors = require('cors')({
  origin: '*'
});

但不要在任何地方注入它(除非那是...一部分)

某處應該有這樣的東西

app.use(cors())

(或者你的框架是怎么做的)

在再次嘗試使用瀏覽器之前,您可以檢查 postman 中的響應標頭以查看您要查找的 cors 標頭是否存在:

像這樣:

Access-Control-Allow-Origin: *

暫無
暫無

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

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