【发布时间】:2017-02-26 04:15:26
【问题描述】:
我正在使用React + Redux 开发一个应用程序,并且我的JSON 数据库位于Firebase 数据库中。
为此,我正在尝试从有效的 URL(通过 Firebase 模拟器验证)fetch 我的数据
let firebase = 'https://******.firebaseio.com/**/*/***/*'
return (dispatch) => {
return fetch(firebase)
.then(res => res.json())
.then(json => dispatch({ type: 'GET_JSON', payload: json }))
}
这会返回给我的错误:
Fetch API 无法加载 https://console.firebase.google.com/project/****/database/data/**/*/***/*。从 'https://console.firebase.google.com/project//database/data/**///' 到 'https://accounts.google.com/ServiceLogin?ltmpl=firebase&osid=1&passive=true…ole.firebase.google.com/project//database/data///**/' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。`
我尝试了很多解决方案,比如添加到 fetch 的第二个参数 { mode: 'no-cors', credentials: 'same-origin'},但是当我尝试这个时,我得到了 Uncaught (in promise) SyntaxError: Unexpected end of input。
我错过了什么?
【问题讨论】:
-
我遇到了同样的问题,我通过将
{ method: 'GET', mode: 'cors', headers: new Headers({ 'Content-Type': 'application/json' }传递给fetch作为第二个参数来解决它。
标签: javascript reactjs firebase redux cors