简体   繁体   中英

blocked by CORS policy: No 'Access-Control-Allow-Origin' header in react / node

For some reason I am getting the error

blocked by CORS policy: No 'Access-Control-Allow-Origin' header

from my code here:



import * as firebase from "firebase";

function FreshMail() {
  const db = firebase.firestore();
  firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      let user = firebase.auth().currentUser.uid;

      let cityRef = db.collection("Users").doc(user);
      let getDoc = cityRef
        .get()
        .then((doc) => {
          if (!doc.exists) {
            console.log("No such document!");
          } else {
            console.log("Document data:", doc.data());

            var request = require("request");

            let data = doc.data();

            var dataString = {
              userE: data.UserEmailAddress,
              userP: data.UserEmailPassword,
              userH: data.UserEmailHost,
              userPort: data.UserEmailPort,
            };

            var headers = {
              "Content-Type": "application/json",
              "Access-Control-Allow-Origin": "*",
              "Access-Control-Allow-Methods": "GET, PUT, POST, OPTIONS",
              "Access-Control-Allow-Headers": "*",
            };

            var options = {
              url:
                "https://us-central1-zenmail-bfd57.cloudfunctions.net/***",
              method: "POST",
              body: dataString,
              headers: headers,
            };

            function callback(error, response, body) {
              if (!error && response.statusCode == 200) {
                console.log(body);
              }
            }

            request(options, callback);
          }
        })
        .catch((err) => {
          console.log("Error getting document", err);
        });
    }
  });
}

export default FreshMail;


I have tried adding the access control allow origin to the header but even that does not seem to be working. I have tried running it from localhost and from my firebase hosting and am getting the error on both. Any help would be appreciated!

I had the same issue and this article might help you out with that

https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9

I used https://cors-anywhere.herokuapp.com/https://nameofapisite.com and it worked fine for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM