How to store results of search for display (wikipedia project)?

How to store results of search for display (wikipedia project)?
0

#1

I can successfully search the wikipedia api with the following script:
$.ajax({

url: 'https://en.wikipedia.org/w/api.php?',
type: 'GET',
dataType: 'jsonp',
data: {
 action: 'opensearch',
 format: 'json',
 search: usr_search,
},
success: function (data) {
 $('p').append(data[3]);
}

});

The only thing is, what is the best way to store some number of results for display on the page? Is it best to put them into an array? If so, how can I separate one result article from the next and store them for display later? Any specific examples would be great!
Thanks!


#2

What is it that you’re wanting to do with this data? I would advise against keeping data around unless you absolutely must.


#4

I need to keep the data I get from the JSONP call so that I can display it on the page (just like on the fcc wikipedia search viewer project). I am just unsure of how to split the data up like I would if I had been able to use JSON and stringify(). Basically, I need to find a way to get the search term and a link to appear for each result after a user searches a given term. What is the best way to do this? Any specific examples would be great!
Thanks!


#5

Where do you want the result to appear? It looks as though you are placing it in a paragraph.

Yes, place them in an array, and create a container for the results. You will want the title to connect to the wiki page and bring in at least the first sentence.

You can loop it for an un-ordered list and style each li with borders as separators - or call on paragraphs as you seem to be doing.

Why display later? Don’t you want instant gratification?

You can google to search for some great examples from other FCC coders, and learn from the various styles they use.


#6

I currently have the whole thing appearing in a paragraph because I wasn’t sure how to split up the data. Before now, I was just focusing on correctly getting the data, so the setup is a preliminary one. The problem is that when I try and return parts of the data variable, I don’t seem to get anything useful that I can split up. If anyone could help me with the syntax accessing the parts of the data that is coming in to split up and display, that would be great!
Thanks!


#7

I’m not sure how much this will help… https://codepen.io/KoniKodes/full/begdjo/

It works fine except when I type in something that doesn’t have a page (hawaii royalty instead of royal hawaii).

I hope this will lead you in the right direction though.


#8

My advice is to use a different query to the Wikimedia API. It isn’t well documented, but through a lot of trial and error, I came up with a different set of parameters that makes searching much easier. I made a quick demo which should explain the relevant details. KoniKodes has a fantastic project, but you may want a reference that’s a little more bare-bones.


#9

Thanks for the help!


#10

Thanks! This way looks better than the way I was attempting earlier. I’ll let you know what happens with this method.
Thanks again!