views:

518

answers:

4

I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest). If I type the url in the address bar of Firefox, I get a neat XML, no sweat there. Yet, calling the exact same url with:

// this is my XMLHttpRequest object
httpObjectMain.open("GET", "http://en.wikipedia.org/w/api.php?action=query&format=xml&prop=langlinks&lllimit=500&titles=kaas", true);
httpObjectMain.send(null);

returns an empty response. According to FireBug, I get a 200 OK response, but the content is just empty.

I suspect I might be missing something on the header of the GET http request.

Help! (and thanks!)

Edit I have finished the implementation, and you can see it for yourself. The code is also available, for those of you struggling with this issue.

+2  A: 

The browser will not allow you to send an XHR to another domain other than the one the page is on. This is for security purposes.

One way around this that I have seen is to setup a proxy on the domain the page is hosted on that will pass requests through to the actual api server. See http://ajaxpatterns.org/Cross-Domain_Proxy

David Hogue
+2  A: 

The Wikipedia API does support JSONP. Your query string'll become something like this:

http://en.wikipedia.org/w/api.php?action=query&format=json&callback=test&prop=langlinks&lllimit=500&titles=kaas

But you'll have to build the jsonp handler (or you can use your favorite library to do it), switch to json output format from the xml you choose and create the callback function to parse the result and do the stuff you need on the page.

EDIT: typo

Marco Z
Thanks, this sounds like a path to be explored.
Fred Rocha
Keep in mind that with JSONP technically you aren't using an XMLHttpRequest but a script injection technique.
Marco Z
Worked for me! Parsing the returned JSON was another adventure, yet the data was returned just fine.
Fred Rocha
A: 

Can you guys please tell me the simplest way to pull wikipedia data given a certain search criteria? I have a quote of the day section on my website and I am trying to do a popup on mouseover of the author's name that shows information about that person (including a photo) pulled from Wikipedia.

Any help would be appreciated!

Matt Cashatt