在本地调用 Firebase 云函数时,我收到 200 响应,但响应是 cors 类型响应,而不是我的实际数据

Posted

技术标签:

【中文标题】在本地调用 Firebase 云函数时,我收到 200 响应,但响应是 cors 类型响应,而不是我的实际数据【英文标题】:When calling Firebase cloud function locally I receive a 200 response but the response is a cors type response not my actual data 【发布时间】:2019-04-18 00:34:07 【问题描述】:
Response type: "cors", url: "Function url with params", 
redirected: false, status: 200, ok: true, …
body: ReadableStream
bodyUsed: false
headers: Headers 
ok: true
redirected: false
status: 200
statusText: ""
type: "cors"
url: "function url with params"
__proto__: Response

调用函数:

export function authDB(token,cb)
fetch(`$apiURLauth?token=$token`,
    method:"GET"
    )
    .then(cb)
    .catch(error=>
        console.log(error)
    )

我已在我的云功能中设置响应以允许 cors

response.set('Access-Control-Allow-Origin', "*")   
response.set('Access-Control-Allow-Methods', 'GET, POST')

使用 Postman 进行测试时,所有响应场景都是实际数据

【问题讨论】:

提供完整的响应标头将有助于正确回答。另请提及请求方法为 GET 或 OPTIONS 请求。 OPTIONS 请求用于在响应标头中提供 cors 配置。确保您不会对 GET 和 OPTIONS 请求感到困惑 【参考方案1】:

想通了!我需要解析对 Json 的响应。

.then(res=>res.json())
.then(cb)

【讨论】:

以上是关于在本地调用 Firebase 云函数时,我收到 200 响应,但响应是 cors 类型响应,而不是我的实际数据的主要内容,如果未能解决你的问题,请参考以下文章