[英]how to fetch firebase storage json file from client side
I saved a json file in google cloud storage, I want to fetch the file, read the json content and save the content to client indexeddb.我在谷歌云存储中保存了一个json文件,我想获取文件,读取json内容并将内容保存到客户端indexeddb。
loadJSON(function(json) {
console.log(json); // this will log out the json object
});
var fileUrl ="https://firebasestorage.googleapis.com/v0/b/rwrwr-rwr.appspot.com/o/TestCatalog.json?alt=media&token=r545-54-54-54-45455"
function loadJSON() {
var request = new Request(fileUrl);
fetch(request, {mode: 'cors'}).then(function(response) {
// Convert to JSON
return response.json();
}).then(function(j) {
// Yay, `j` is a JavaScript object
console.log(JSON.stringify(j));
}).catch(function(error) {
console.log('Request failed', error)
});
}
The fetch is producing this error提取正在产生此错误
from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
CORS 策略已阻止来自 origin 'null' 的请求:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
How could I fetch the firebase storage json file ?我怎样才能获取 firebase 存储 json 文件?
If you're running this code form the browser you have to enable cross-origin access on your Cloud Storage bucket.如果您在浏览器中运行此代码,则必须在 Cloud Storage 存储分区上启用跨域访问。
For that you can use the gsutil tool und run为此,您可以使用gsutil工具并运行
gsutil cors set cors.json gs://<your-cloud-storage-bucket>
The cors.json file should contain the following content: cors.json 文件应包含以下内容:
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.