Issue with Wikipedia viewer: No 'Access-Control-Allow-Origin' header is present on the requested resource

Hello World!
I’m having issues with my Wikipedia Viewer.
I am calling for the JSON of this page: "https://en.wikipedia.org/w/api.php?action=query&list=prefixsearch&pssearch=" +value +"&prop=revisions&rvprop=content&format=json&formatversion=2" where ‘value’ is a variable for the search term. When I go to this website in my web browser (with an included search term), I get a page a lot like this (depending on the search term):
{"batchcomplete":true,"continue":{"psoffset":10,"continue":"-||revisions"},"query":{"prefixsearch":[{"ns":0,"title":"T","pageid":30071},{"ns":0,"title":"Tilde","pageid":212390},{"ns":0,"title":"Taxonomy (biology)","pageid":30463},{"ns":0,"title":"Time zone","pageid":30890},{"ns":0,"title":"Turkey","pageid":11125639},{"ns":0,"title":"The New York Times","pageid":30680},{"ns":0,"title":"Town","pageid":52911},{"ns":0,"title":"Taiwan","pageid":25734},{"ns":0,"title":"Thailand","pageid":30128},{"ns":0,"title":"Television show","pageid":19508643}]}}

However, my code wasn’t working. I went to the console and found this message: Failed to load https://en.wikipedia.org/w/api.php?action=query&list=prefixsearch&pssearch=t&prop=revisions&rvprop=content&format=json&formatversion=2: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin [LINK REMOVED] is therefore not allowed access.

Anyone have any ideas?

(Full code:

$.getJSON(
    "https://en.wikipedia.org/w/api.php?action=query&list=prefixsearch&pssearch="+value+"&prop=revisions&rvprop=content&format=json&formatversion=2",
    function(json) {
       //code goes here
   });

)

I’ve found a solution, but it isn’t a good one. I installed this extension for chrome, which allowed the page to work. However, I would prefer another solution, because this requires anyone who wants to access my site to have installed the extension.

Hint: Cross-Origin Resource Sharing, Stack overflow, optionally JSONP

Put &origin=* in the URL.

1 Like

Thank you so much.
Who would have thought that such a little snippet of code:

&origin=*

could cause so much pain and suffering?

1 Like