React JS使用API​​上传图像

发布于 2025-01-20 20:45:05 字数 591 浏览 1 评论 0原文

因此,我正在尝试使用React JS Ant设计上传组件上传文件,现在我面临的问题是,每当我尝试提交图像时,我都会收到以下错误:

访问'https://freeimage.host/api /1/upload'从原始''http:// localhost:3000'被CORS策略阻止:没有“访问control-wall-allow-origin”标题在请求的资源上存在。如果不透明的响应满足您的需求,请将请求模式设置为“无库”,以通过禁用CORS来获取资源。

我已经附上了我编写的代码上传和处理文件提交的代码。

​/I.sstatic.net/si1nb.png“ alt =”在此处输入图像描述”>

So I am trying to upload a file using React Js ant design upload component now the issue I am facing is that whenever I try to submit an image I received the following error:

Access to fetch at 'https://freeimage.host/api/1/upload' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I have attached the code that I have written to upload and handle file submissions.
enter image description here

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

素食主义者 2025-01-27 20:45:05

我将尝试将标题添加到您的Handlesubmission Arrow功能中的获取并串起身体。

fetch('https://freeimage.host/api/1/upload', {
  key: apiKey,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData),
})

I would try adding headers to the fetch inside your handleSubmission arrow function and stringify the body.

fetch('https://freeimage.host/api/1/upload', {
  key: apiKey,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData),
})
笑着哭最痛 2025-01-27 20:45:05

如果您使用后端作为 Node js,请添加以下代码

const cors = require("cors");
const corsOpts = {
origin: "*",
  credentials: true,
};

app.use(cors(corsOpts));

If you are using your backend as node js add following code to it

const cors = require("cors");
const corsOpts = {
origin: "*",
  credentials: true,
};

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