【问题标题】:Angular $http.get with firebaseAngular $http.get 与 firebase
【发布时间】:2017-08-01 23:05:13
【问题描述】:

我正在尝试使用 $http 服务从我的 firebase 数据库中访问信息。这是我的代码:

$http.get("my firebase url").then(function(res) {
    console.log(res);
});

但我得到一个 cors 错误:

XMLHttpRequest cannot load https://console.firebase.google.com/project/...
Redirect from 'https://console.firebase.google.com/project/...' to
'https://accounts.google.com/ServiceLogin?passive=44&osid=1&continue=ht…
owup=https://console.firebase.google.com/project/.../database/data/' 
has been   blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on   the requested resource. Origin 'null' is therefore not allowed access.  

我尝试在谷歌上搜索该问题,但找不到任何我理解的内容。

注意:我是火力基地和有角的菜鸟。

【问题讨论】:

  • 我会检查一下,但它似乎没有使用我想要使用的 angular $http 服务。
  • 这是一个基于 HTTP 的通用“问题”。它确实与所有使用 HTTP 的技术相匹配。
  • 从错误消息看来,您正在尝试从 Firebase 控制台读取数据。那是行不通的。如果要从数据库中读取数据,请确保 my firebase url 的格式为 https://yours.firebaseio.com/,您可以从 Firebase Database console 中复制该格式。
  • 我有正确的 firebase url,我编辑了 url 和错误消息以隐藏敏感信息。

标签: javascript angularjs firebase-realtime-database xmlhttprequest


【解决方案1】:

我希望这会有所帮助。

您需要考虑 3 件事:
1. 在 URL 上使用您的 FB 域而不是控制台“例如:https://YOUR_DOMAIN.firebaseio.com/..”
2. 最后,您需要将 url 扩展为 .json "Ex: ../settings/app .json "
3. 您需要添加相应的规则以允许访问:
例如:

{
    "rules": {
        ".read": "auth != null",
        ".write": "auth != null",
        "settings": {
            "app": {
                ".read": true,
                ".write": false
            }
        }
    }
}

然后你可以使用你的 $http,就像我的例子:

$http.get("https://YOUR_DOMAIN.firebaseio.com/settings/app.json")

此处的示例:https://jsfiddle.net/moplin/1124204w/

【讨论】:

  • 成功了!我不知道我必须将 .json 放在通话网址的末尾。非常感谢:)
  • 很好,它有帮助。这也让我很头疼。
猜你喜欢
  • 2016-11-23
  • 2015-04-09
  • 1970-01-01
  • 2019-01-01
  • 2017-02-15
  • 2019-03-21
  • 2015-08-09
  • 2016-05-03
  • 1970-01-01
相关资源
最近更新 更多