无法从Wikipedia API获取数据 [英] Cannot fetch data from Wikipedia API

查看:114
本文介绍了无法从Wikipedia API获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let dataObj = [];
const query = 'marvel';
fetch(`https://en.wikipedia.org/w/api.php?action=query&titles=${query}&prop=revisions&rvprop=content&format=json&formatversion=2`)
  .then(data => data.json())
  .then(data => dataObj.push(data))
  .catch(err => console.log(err));

这是我收到的错误:

请求的资源上没有' Access-Control-Allow-Origin '标头。因此,不允许访问 https://s.codepen.io 。如果不透明响应满足您的需求,请将请求的模式设置为 no-cors 以获取禁用CORS的资源。

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://s.codepen.io' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我将模式设置为 no-cors 但仍然没有运气。

I set the mode to no-cors but still no luck.

推荐答案

尝试添加查询参数 origin = * 以帮助解决CORS问题:

Try adding the query parameter origin=* to help resolve the CORS issue:


https: //en.wikipedia.org/w/api.php?action=query&titles= $ {查询}&安培;丙=修订&安培; rvprop =含量&安培;格式= JSON&安培; formatversion = 2及原点= *

https://en.wikipedia.org/w/api.php?action=query&titles=${query}&prop=revisions&rvprop=content&format=json&formatversion=2&origin=*

希望这有帮助!

这篇关于无法从Wikipedia API获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆