使用 fetch 获取数据时出现 CORS 错误

Getting CORS Error when using fetch to get data

我正在尝试访问 API 以获取一些数据,但我一直收到此 CORS 错误。我已经检查了我的代码是否有任何语法错误,但我找不到任何错误。我附上了一张错误图片和我应该获取数据的函数。

async function getData(){
  const request = await fetch('https://api.igdb.com/v4/games', {
    method: 'POST',
    headers: {
    'Client-ID': 'jglmao8u28qo1p9wltqne325i7xh3u',
    'Authorization': 'Bearer 4xau27m6liukizor4z2l8mlb7vbpjk',
  }
  })
  const response = await request.json();
  console.log(response);
}

有一个很棒的代理专门用于此 - 绕过 CORS 块。源代码在这里:https://github.com/Rob--W/cors-anywhere,你可以这样使用它:

https://cors-anywhere.herokuapp.com/https://api.igdb.com/v4/games 基本上只是在实际图像 URL.

之前添加 CORS-Anywhere URL

在你的情况下,应该是

async function getData(){
  const request = await fetch('https://cors-anywhere.herokuapp.com/https://api.igdb.com/v4/games', {
    method: 'POST',
    headers: {
    'Client-ID': 'jglmao8u28qo1p9wltqne325i7xh3u',
    'Authorization': 'Bearer 4xau27m6liukizor4z2l8mlb7vbpjk',
  }
  })
  const response = await request.json();
  console.log(response);
}

如果您受到该网站的速率限制,请尝试 https://circumvent-cors.herokuapp.com/,这是我从 GitHub 源代码部署的一个,没有修改,我认为它不应该限制您的速率.

干杯,让我知道这是否有效。