Firebase提取-禁止访问控制-允许起源 [英] Firebase Fetch - No Access-Control-Allow-Origin

查看:45
本文介绍了Firebase提取-禁止访问控制-允许起源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 React + Redux 开发一个应用程序,并且在 Firebase 数据库中有我的 JSON 数据库.为此,我尝试从有效的URL(已通过Firebase模拟器验证)获取我的数据

I'm developing an app with React + Redux and I have my JSON database within a Firebase DB. To do this I'm tryin to fetch my data from a valid URL (validated from Firebase simulator)

let firebase = 'https://******.firebaseio.com/**/*/***/*' 
    return (dispatch) => {
        return fetch(firebase)
            .then(res => res.json())
            .then(json => dispatch({ type: 'GET_JSON', payload: json }))
    }

这向我返回错误:

Fetch API无法加载/database/data///** /"已被CORS策略阻止:所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始空".如果不透明的响应满足您的需求,请将请求的模式设置为"no-cors",以在禁用CORS的情况下获取资源.

Fetch API cannot load https://console.firebase.google.com/project/****/database/data/**/*/***/*. Redirect from 'https://console.firebase.google.com/project//database/data/**///' to 'https://accounts.google.com/ServiceLogin?ltmpl=firebase&osid=1&passive=true…ole.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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`

我尝试了许多解决方案,例如将fetch的第二个参数添加到 {模式:'no-cors',凭据:'same-origin'} ,但是当我尝试这样做时,我得到了未捕获(承诺)SyntaxError:输入意外结束.

I've tried many solutions, like adding to fetch's second argument { mode: 'no-cors', credentials: 'same-origin'}, but when I try this i get Uncaught (in promise) SyntaxError: Unexpected end of input.

我想念什么?

推荐答案

likely error that arise to  cors blocked when using firebase is
when you initiate a put or get request with an incomplete firebase url
e.g
// wrong form
 this.http.get('https://******.firebaseio.com/data')   //this will throw an exception

// correct form                                                    
  this.http.get('https://******.firebaseio.com/data.json')

这篇关于Firebase提取-禁止访问控制-允许起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆