0

我尝试使用 Riot 游戏 API,下面的代码返回了“状态代码:200”,看起来还不错,然后我收到了两个错误,如下所示。

Access to fetch at 'https://oc1.api.riotgames.com/lol/summoner/v4/summoners/by-name/edisona?api_key=RGAPI-xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' from origin 'http://127.0.0.1:5500' 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.

GET https://oc1.api.riotgames.com/lol/summoner/v4/summoners/by-name/edisona?api_key=RGAPI-xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx net::ERR_FAILED

所以我添加{ mode: 'no-cors' }并且错误消失了,但是之后控制台中没有返回任何数据我只是直接使用URL并且浏览器显示正确的数据。我不知道为什么会这样,如果你能帮助我,我很感激。

const name = 'edisona';
const api_key = 'RGAPI-xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const url =
  'https://oc1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' +
  name +
  '?api_key=' +
  api_key;
fetch(url)
.then(function(response) {
  console.log(response); 
}).catch(function(error) {  
  console.log('Request failed', error)  
});
4

1 回答 1

0

这似乎是一个跨组织请求错误(您没有将 CORS 标头添加到代理请求中)。

尝试使用CORS-Anywhere,一个 NodeJS 反向代理来做到这一点。

这是一个演示。尝试阅读一些关于 CORS 的内容:https ://fetch.spec.whatwg.org/#cors-protocol-and-credentials

于 2020-11-20T07:28:58.823 回答