1
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.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Aditya Raj
  • 327
  • 3
  • 14

1 Answers1

4

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!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Plz don't mind me asking but, What is origin here. Do i have to leave it = *- or i need to get the value from somewhere – Aditya Raj Feb 04 '18 at 05:40
  • Check out this question, it desrcibes CORS and the Wikipedia API https://stackoverflow.com/q/23952045/5059657 . You effectively are setting an origin header to match any source by `origin=*`. For not authenticated requests this will be enough to get the data you need. Try it and see how it works for you. – Alexander Staroselsky Feb 04 '18 at 19:10
  • If you were doing an authenticated request. You'd have to set the origin to match your server/client location in line with the API account you'd create. `origin=*` would be enough for your non authenticated basic data retrieval. – Alexander Staroselsky Feb 04 '18 at 19:17