我有一个 Javascript Web 应用程序。我尝试读取或写入 Cloud Firestore 数据库,但它在浏览器控制台中返回此错误。
错误:
Failed to load https://firestore.googleapis.com/google.firestore.v1beta1.Firestore/Write/channel?VER=8&RID=21505&CVER=21&X-HTTP-Session-Id=gsessionid&%24httpHeaders=X-Goog-Api-Client%3Agl-js%2F%20fire%2F4.8.2%0D%0Agoogle-cloud-resource-prefix%3Aprojects%2FProjectName%2Fdatabases%2F(default)%0D%0A&zx=v9xcdtq156tr&t=1:
The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.
安全规则设置为公共(public)读/写
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
这是我的测试代码
<script src="https://www.gstatic.com/firebasejs/4.11.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.11.0/firebase-firestore.js"></script>
<script>
var config = {
apiKey: "XXXXXXXXXXX",
authDomain: "XXXXXXXXXXXX",
databaseURL: "XXXXXXXXXXXX",
projectId: "XXXXXXXXXXXX",
storageBucket: "XXXXXXXXXXXX",
messagingSenderId: "XXXXXXXXXXXX"
};
firebase.initializeApp(config);
// Firebase Variables
var db = firebase.firestore();
db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA"
}).then(function () {
console.log("Document successfully written!");
}).catch(function (error) {
console.error("Error writing document: ", error);
});
</script>
最佳答案
Google Chrome 版本 65.0.3325.162 是问题所在。 已使用 Microsoft Edge 进行测试并且有效
关于javascript - Cloud Firestore 读/写错误 : The 'Access-Control-Allow-Origin' header has a value 'null' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49336294/