Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wikipedia API + Cross-origin requests

Tags:

I'm trying to access Wikipedia using JavaScript and CORS.

As far as I know, Wikipedia should support CORS: http://www.mediawiki.org/wiki/API:Cross-site_requests

I tried the following script: create a XMLHttpRequest+credential/XDomainRequest, add some HTTP headers ("Access-Control-Allow-Credentials", etc.) and send the query.

http://jsfiddle.net/lindenb/Vr7RS/

var WikipediaCORS=     {         setMessage:function(msg)         {             var span=document.getElementById("id1");             span.appendChild(document.createTextNode(msg));         },         // Create the XHR object.         createCORSRequest:function(url)         {             var xhr = new XMLHttpRequest();              if ("withCredentials" in xhr)             {                 xhr.open("GET", url, true);             }             else if (typeof XDomainRequest != "undefined")             {                 xhr = new XDomainRequest();                 xhr.open(method, url);             }             else             {                 return null;             }             xhr.setRequestHeader("Access-Control-Allow-Credentials", "true");             xhr.setRequestHeader("Access-Control-Allow-Origin", "*");             return xhr;         },         init:function()         {             var _this = this;             var url = 'http://en.wikipedia.org/w/api.php?action=opensearch&search=Javascript&format=json';             var xhr = this.createCORSRequest(url);             if (!xhr)             {                 this.setMessage('CORS not supported');                 return;             }              xhr.onload = function()             {                 _this.setMessage(xhr.responseText);             };             xhr.onerror = function()             {                 _this.setMessage('Woops, there was an error making the request.');             };             xhr.send();         }     }; 

But my script fails ('xhr.onerror' is called). How can I fix it?

like image 277
Pierre Avatar asked May 30 '14 10:05

Pierre


People also ask

What is a cross-origin request?

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. A web page may freely embed cross-origin images, stylesheets, scripts, iframes, and videos.

Is Wikipedia API free?

Wikipedia and other Wikimedia projects are free, collaborative repositories of knowledge, written and maintained by volunteers from around the world. The Wikimedia API gives you open access to add this free knowledge to your projects and apps.

Can you bypass CORS?

CORS is essentially controlled by the Access-Control-Allow-Origin (ACAO) header on server, and nothing you do on the client can bypass this restriction.


1 Answers

I had the same problem while working on a freeCodeCamp project and the solution was so simple it made me laugh, since I had spent hours searching for it. In your jQuery URL include the parameter below.

&origin=*

Working CodePen

$.getJSON(   'https://en.wikipedia.org/w/api.php?action=query&format=json&gsrlimit=15&generator=search' +   '&origin=*' + // <-- this is the magic ingredient!   '&gsrsearch='q, function(data){ /* ... */ } ); 
like image 164
luckyguy73 Avatar answered Sep 23 '22 21:09

luckyguy73