Cannot fetch data from Wikipedia API

早过忘川 提交于 2020-01-21 10:11:45

问题


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));

This is the error that I receive:

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.

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


回答1:


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

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

Hopefully that helps!



来源:https://stackoverflow.com/questions/48604705/cannot-fetch-data-from-wikipedia-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!