The Wikipedia API does not support CORS Requests

There is an open ticket here: https://phabricator.wikimedia.org/T62835

If you wish to use the Media Wiki API, you have to use JSONP format.

I understand many people are confused about this, I was as well. How can I suggest to freeCodeCamp they do something about this? They should really include this information on their Wikipedia Viewer challenge, as this information runs contrary to this link: https://www.mediawiki.org/wiki/API:Cross-site_requests that freeCodeCamp puts up on the challenge page.

Media Wiki claims that CORS has been working since 2012, but clearly this is not true for external sites such as Codepen.

3 Likes

As of today, Wikipedia supports CORS requests by using the normal ajax requests (no need of JSONP/callback manipulation).
This can be done by setting the origin in the API call.


For authenticated requests, this must match the one of the one of the “origins” in the Origin header exactly (you need to set this using the beforeSend property while making an ajax call).

For unauthenticated requests, you can simply set it as an asterisk (*), and it works when using a simple $.getJSON from your domain.
Example api call:

https://en.wikipedia.org//w/api.php?action=opensearch&format=json&origin=*&search=stack&limit=10


More at the MediaWiki API sandbox: MediaWiki API sandbox

4 Likes

This was incredibly helpful! I am using the vanilla javascript XmlHttpRequest method and it does not allow explicity setting the Origin header. Seeing it’s possible to set it in the query string was pure gold!

Thank you!

Kyle