I’ve gotten half way with the Wikipedia Viewer API project but am stuck on retrieving JSON from parsed data in separate chunks.
I wanted to make my search a bit more dynamic so I decided to have search results appear as the user types into the input box. The results are being output successfully onto a DIV called ‘output’ but as one piece, and I can’t seem to slice the results individually on the page using Javascript. I can only output and reference two JSON variables, which result in [Object Object] and can’t seem to pinpoint the ID field, which is dynamically created as the user types into the input box. So as the user puts in their text the JSON url is created on the fly and the field coming after “query”:{“pages”: is usually a dynamic number field that is the label I can’t grab with Javascript.
This project is still unfinished so you’ll see a submit button that doesn’t yet work and some other things.
The dynamic Javascript variable I am using to store the URL is:
var urlsearchT = “https://en.wikipedia.org/w/api.php?action=query&format=json&origin=*&generator=search&gsrsearch=” + up + “&gsrnamespace=0&gsrlimit=10&gsroffset=0&gsrwhat=text”;
Essential issue is when I try to ID the JSON field within the populate() function using this variable:
var title = data.query.pages I don’t know what identifier to use to ID the “query”:{“pages”: which is created dynamically as user types into input box. I could use a loop after I stringify() my JSON and then ID page # that way but that seems really long and unnecessary.
Here is my codepen:
https://codepen.io/osalvatierra/pen/NwjzMX
Any help or suggestions greatly appreciated.
Best,
Oscar