Resolved - Wiki viewer -Don't understand how to get json info from API - Spolier

Resolved - Wiki viewer -Don't understand how to get json info from API - Spolier
0.0 0

#1

Hi all, I’m working on the wiki viewer and have been stuck for a little while. Despite reading and searching for info, I still don’t fully understand how to get json info from the wikipedia api.

My function is below. Any pointers, tips or suggestions about where or what I might be doing wrong will be appreciated.

function search(){
 var searchWord = $("#searchbox").val();
 var apiResult = "https://www.mediawiki.org/w/api.php?action=query&format=json"
 + searchWord;

 $.ajax({
url:apiResult,
dataType:"json",
success: function(info){
console.log(info)
}
//selector to display info to go here
 }


#2

Do you have this in Codepen? It’s easier for other to hack around and see problems with live code.


#4

I should add that I used some different values than you are, starting with the API’s URL (https://en.wikipedia.org/w/api.php).

My get function pulls info from the following object:

apiDetails = {
  endpoint: 'https://en.wikipedia.org/w/api.php',
  params  : {
    action: 'query',
    format: 'json',
    prop: 'pageimages|extracts|info',
    inprop: 'url',
    generator: 'search',
    gsrsearch: searchTerm,
    gsrlimit: 10,
    exintro: 1,
    exlimit: 'max',
    explaintext: 1,
    grnnamespace: 0,
    grnfilterredir: 'nonredirects',
    origin: '*'  // <-- see this guy here?
  }
};

Note the last property in the params object: origin: '*'. You need to specify origin with each request. This would seem to stop the whole project dead in its tracks, except you can specify origin: '*' which the API accepts as an anonymous request! begs the question: why even require it?!

More info:
https://www.mediawiki.org/wiki/Manual:CORS


#5

Hi @belcurv,
Thanks for your reply. I’ve added everything to codepen here

With a little tweaking, I’m getting search results from the api, although I’m also getting an error message from the object. May have something to do with what you’ve mentioned above, but I have a headache now. lol :smile:


#6

Hmm. I’m not getting errors.


#7

Thanks for having a look @belcurv,:thumbsup:
It’s working fine, :slight_smile: the error I saw was to do with the empty search box.