【发布时间】:2023-03-05 19:49:01
【问题描述】:
我正在尝试使用 Fetch API 从 Service Worker 查询 Firebase 数据库。但是它不能按预期工作,因为我无法正确验证。
基本上我想要做的是来自 Service Worker 内部的 origin https://myproject.firebaseapp.com 我做了这样的调用:
var fetchOptions = {};
fetchOptions.credentials = 'include';
var url = options.messageUrl;
var request = new Request('https://myproject.firebaseio.com/user/foobar.json', fetchOptions);
messagePromise = fetch(request).then(function(response) {
return response.json();
});
我收到此错误:
Fetch API cannot load https://myproject.firebaseio.com/user/foobar.json. Response to preflight request doesn't pass access control check: Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'https://myproject.firebaseapp.com' is therefore not allowed access.
有解决方法的想法吗?如何从 SW 查询/更新 Firebase 数据库?
我已经阅读了https://jakearchibald.com/2014/using-serviceworker-today/,其中一个问题正是这个问题,即 Fetch 请求不发送身份验证这一事实。
理想情况下,能够在 SW 中使用 Firebase JS API 会很棒,但这似乎也无法正常工作。
【问题讨论】:
标签: firebase firebase-realtime-database firebase-authentication service-worker fetch-api