使用firebase的Angular $ http.get

时间:2017-03-11 17:05:23

标签: javascript angularjs firebase-realtime-database xmlhttprequest

我正在尝试使用$ 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.  

我试过谷歌搜索问题,但我找不到任何我理解的东西。

注意:我是一个firebase和角度noobie。

1 个答案:

答案 0 :(得分:2)

我希望这会有所帮助。

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

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

然后您可以使用$ http,如我的示例所示:

$http({
  method: 'GET',
  url: '/home/modulesInfo'
}).then(function(response) {
  console.log('data: ' + JSON.stringify(response.data));
  result1 = response.data;
}, function(error) {

});

enter image description here

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