javascript - Firebase Fetch - 没有访问控制允许来源

标签 javascript reactjs firebase redux cors

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

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

这会给我返回错误:

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 的第二个参数 { mode: 'no-cors', credentials: 'same-origin'},但是当我尝试这个时我得到了 未捕获( promise )语法错误:输入意外结束

我错过了什么?

最佳答案

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')

关于javascript - Firebase Fetch - 没有访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081984/

相关文章:

javascript - 将 firebase-messaging-sw.js deps 与 Webpack 捆绑在一起

javascript - Firebase 触发器功能交替工作

javascript - 第一次点击时显示开始标签,第二次点击时显示结束标签

javascript - 如何在 Magento 中使用像 head.js 或 labjs 这样的 JavaScript 加载器

javascript - 如何通过对象 ID 访问其他属性值?

reactjs - 无法在类组件 React.js 中使用 useState

javascript - 为什么我没有收到(网络)请求时显示的适当错误消息?

javascript - 默认情况下选中的复选框

reactjs - 如何将 JSX 文件引用到 HTML 页面

javascript - Firestore 一个范围查询,在不同的字段上排序