使用OnRequest云功能时的CORS错误
这是我
export const testFunction = functions.https.onRequest((req, res) => {
const text = req.body.text;
res.set("Access-Control-Allow-Origin", "*");
res.send({text: text});
});
尝试使用const cors = require('cors')的
功能/tvhyr.png“ rel =” nofollow noreferrer“> this 作为响应。有人知道为什么吗?
This is my function
export const testFunction = functions.https.onRequest((req, res) => {
const text = req.body.text;
res.set("Access-Control-Allow-Origin", "*");
res.send({text: text});
});
i've tried using const cors = require('cors')({origin: true});
I keep getting this as a response. Does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑这样的导入,
然后尝试使用
firebase部署 - 仅运行以下功能
:然后使用以下方式发送请求:
我在控制台中获得的输出是:

并且当我单击云功能URL端点时,我的输出是一个空{}。
如果您尝试
res.send(“ Hello world”)
代替res.send({text:text:text})
,您将将输出作为Hello World
中的浏览器中的输出,但是由于我们的云功能使用请求主体中传递的数据执行了一些人为的操作。如果属性名称为null或未定义,则在运行时可能会导致错误。确实,如果我们部署了此新功能,然后尝试从我们的Web应用程序调用它而不更新我们的请求,我们会遇到错误。但是,这可能不是您期望的错误。
很容易假设我们以某种方式错误地配置了我们的CORS政策。 Infact交换
cors()
tocors({onect:'*'})
tocors({origan:arein:true})
无用。只有当我们查看云功能的日志时,才会获得有用的错误消息。因此,请尝试发送带有–DATA标志的发布请求,或者如果您使用的是Postman,请发送数据和参数。然后,只有您才能输出,如果您仍然会看到CORS错误,请检查您的功能是否处理得很好,或者您的嵌套请求主体属性不是未定义的/null。有时候,CORS错误并不总是CORS!
Consider importing like this,
And try running the below function using
firebase deploy –only functions
:And then send request using :
The output I am getting in the console is :

And when I click on the Cloud Function URL endpoint, my output is an empty {}.
If you try
res.send(“Hello World”)
in place ofres.send({text:text})
, you will get the output in the browser asHello World
but since our Cloud Function performs some contrived operation using data passed in the request body.This could result in an error at run time if the property name is null or undefined.And indeed, if we deploy this new function and then attempt to call it from our web app without updating our request we do get an error. However, it might not be the error you’d expect.
It’d be easy to assume that we somehow misconfigured our CORS policy. Infact swapping
cors()
tocors({ origin: '*'})
tocors({ origin: true})
all to no avail. Only when we view the logs for our Cloud Function do we get a useful error message.So try sending a POST request with the –data flag, or if you are using Postman, send the data and the parameters. Then only you would be able to have an output, if you still see the CORS error, check if your function is handled well or your nested request body attribute is not undefined/null. Sometimes CORS errors are not always CORS!