使用OnRequest云功能时的CORS错误

发布于 2025-02-08 05:04:24 字数 343 浏览 1 评论 0原文

这是我

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-02-15 05:04:24

考虑这样的导入,

const cors = require('cors')({origin: true});

然后尝试使用firebase部署 - 仅运行以下功能

const functions= require("firebase-functions");
const cors = require('cors')({origin: true});
exports.testFunction = functions.https.onRequest((req, res) => {
 cors(req, res, () => {
   const text = req.body.name;
   res.send({text:text});
 });
});
 

然后使用以下方式发送请求:

curl -X POST "https:/region-projectID.cloudfunctions.net/function-name" -H "Content-Type:application/json" --data '{"name":"Keyboard Cat"}'

我在控制台中获得的输出是:

并且当我单击云功能URL端点时,我的输出是一个空{}。

如果您尝试res.send(“ Hello world”)代替res.send({text:text:text}),您将将输出作为Hello World中的浏览器中的输出,但是由于我们的云功能使用请求主体中传递的数据执行了一些人为的操作。如果属性名称为null或未定义,则在运行时可能会导致错误。
确实,如果我们部署了此新功能,然后尝试从我们的Web应用程序调用它而不更新我们的请求,我们会遇到错误。但是,这可能不是您期望的错误。

很容易假设我们以某种方式错误地配置了我们的CORS政策。 Infact交换cors() to cors({onect:'*'}) to cors({origan:arein:true})无用。只有当我们查看云功能的日志时,才会获得有用的错误消息。

因此,请尝试发送带有–DATA标志的发布请求,或者如果您使用的是Postman,请发送数据和参数。然后,只有您才能输出,如果您仍然会看到CORS错误,请检查您的功能是否处理得很好,或者您的嵌套请求主体属性不是未定义的/null。有时候,CORS错误并不总是CORS!

Consider importing like this,

const cors = require('cors')({origin: true});

And try running the below function using firebase deploy –only functions :

const functions= require("firebase-functions");
const cors = require('cors')({origin: true});
exports.testFunction = functions.https.onRequest((req, res) => {
 cors(req, res, () => {
   const text = req.body.name;
   res.send({text:text});
 });
});
 

And then send request using :

curl -X POST "https:/region-projectID.cloudfunctions.net/function-name" -H "Content-Type:application/json" --data '{"name":"Keyboard Cat"}'

The output I am getting in the console is :
enter image description here

And when I click on the Cloud Function URL endpoint, my output is an empty {}.

enter image description here

If you try res.send(“Hello World”) in place of res.send({text:text}), you will get the output in the browser as Hello 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() to cors({ origin: '*'}) to cors({ 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文