如何使获取API在不启用浏览器中的COR的情况下工作

发布于 2025-02-02 15:33:12 字数 2193 浏览 1 评论 0原文

我一直在尝试将此代码用于各种版本: (URL指向NASA APOD API)

  try {
    const rsp = await fetch(url, {
      method: 'GET',
      mode: 'cors',
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET',
      },
    })
    if (rsp.ok) {
      console.log(`>>> no problem with call to fetch api:`)
      const data = await rsp.json()
      return data
    }
    console.log(`!!! some other problem occurred with call to fetch api:`, rsp)

  } catch (err) {
    console.log(`!!! problem with call to fetch api:`, err)
  }

我一直在遇到此错误:

当我在Chrome中运行代码时。但是,当我在Firefox中运行相同的代码时,我会从API中返回预期数据。

当我在Chrome中安装扩展名以启用CORS时,我会从API中返回预期的数据。

当我使用fetch头 - 'mode'运行代码时:'no -cors',我在chrome中得到以下响应:

我能理解的是要点是因为API在不同的域(IE来自NASA)。 Firefox给出了类似的响应。

因此,我已经达到了可以通过在浏览器中打开COR(通过浏览器扩展程序)来制作Chrome代码的地步。这显然并不令人满意。

我将感谢有关解决Chrome中此问题的任何建议,它确实涉及使用浏览器级别的CORS打开。

最新尝试:

“在此处输入图像描述”

这是NextJS API路由。

这是使用AXIOS调用API路由

“在此处输入图像说明”

这是Axios的错误。

看来代理不起作用,Responseurl看起来它来自本地主机而不是目标API地址。

有什么想法吗?

I have been trying to get this code to work in various versions:
(the URL points to the NASA APOD API)

  try {
    const rsp = await fetch(url, {
      method: 'GET',
      mode: 'cors',
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET',
      },
    })
    if (rsp.ok) {
      console.log(`>>> no problem with call to fetch api:`)
      const data = await rsp.json()
      return data
    }
    console.log(`!!! some other problem occurred with call to fetch api:`, rsp)

  } catch (err) {
    console.log(`!!! problem with call to fetch api:`, err)
  }

I have been getting this error:
enter image description here

when I run the code in Chrome. However, when I run the same code in FireFox, I get the expected data returned from the API.

When I install an extension in Chrome to enable CORS, I get the expected data returned from the API.

enter image description here

When I run the code with the fetch header - 'mode': 'no-cors', I get the following response in Chrome:

enter image description here

which I can understand to a point because the API is on a different domain (i.e. from NASA). FireFox gives a similar response.

So, I have reached the point where I can make the code in Chrome by switching on CORS in the browser (by means of browser extension). This is clearly not satisfactory.

I would appreciate any suggestions on resolving this problem in Chrome that does involve using a browser-level switching on of CORS.

Latest Attempt:

enter image description here

this is the NextJS api route.

enter image description here

this is the function using axios to call the API route

enter image description here

this is error from axios.

It looks like the proxy is not working, the responseUrl looks like it is coming from local host not the target API address.

Any ideas please?

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

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

发布评论

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

评论(1

神魇的王 2025-02-09 15:33:12

从技术上讲,API不属于您,这是无法将其从您的前端调用到该API的方法,不要试图配置您的前端或浏览器,因为它无济于事。

您可以尝试:

  • 检查该API的文档以允许从特定的原始域进行调用。
  • 创建自己的代理服务器,然后不要从前端打电话 - >那个API,您可以称呼“您的前端 - >您的代理 - > the api”(原因,您仍然必须配置代理,如果您的前端不在同一域上运行,

The API doesn't belong to you, technically, it's no way to call that API from your frontend to that API, don't try to config your frontend or browser because it doesn't help.

You can try:

  • Check the document from that API to allow calling from your specific origin domain.
  • Create your own proxy server and then instead of calling from your frontend --> That API, you can call "Your frontend --> Your Proxy --> The API" (Of cause, you still have to config your proxy allow your frontend if they don't running on a same domain)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文