Wikipedia API - getJSON jQuery Help

Hello FreeCodeCamp community!

I have been struggling with the Wikipedia API challenge for a few weeks now. I have taken multiple courses on what APIs are, what GET, POST, PUT, and DELETE are, and how we can use all of them to get access to API info. My question is, how do we then use that info?

For example, I am currently working on my wikipedia API in CodePen and, using https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js as a source, I am trying to get it to spit back some sort of information (really any information). Currently I have:

$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json", function(data) {
        });
    });
});

What sorts of things can I put into the function to get it to show information on screen. I have a DIV setup that I have been trying to insert info into, but have had no luck so far. Here is a link to my CodePen for your review:

Thank you for any info and advice you can give regarding this!

This is the basic URL that I’m using:

$(document).ready(function(){

  var searchString = "apple";
  
  var wikiUrl = 'https://en.wikipedia.org//w/api.php' +
      '?callback=?' +
      '&action=opensearch' +
      '&format=json' +
      '&profile=fuzzy' +
      '&limit=10' +
      '&prop=imageinfo&format=json&iiprop=urll' +
      '&search=' +
      encodeURI(searchString);

  $.getJSON(wikiUrl, function(data) {
    console.log(data);
  });
});

Thank you Kevin! That was very helpful, and I love the format you used to split the URL. So, now that works and I have a bunch of information from the API, but how to I use it? For example, when I search for polar bear, it gives me a list of ten articles with a list of info with titles, then descriptions, then links. So, how do I actually access that information? How is it classified or labeled?

Thank you for any more info you (or anyone else) can provide.

I greatly appreciate it!

Running the code that Kevin gave you seams to give you back an array of arrays. Well, actually the first array item is a string. like this : [“apple”, Array(10), Array(10), Array(10)]. to access the 3rd item in the 2nd array you might have some code like …

is this what you are asking about?

Yes it is! Ironically, I just figured that out about 15 minutes ago and was about to post an update on this forum. Thank you so much! I really appreciate it! I can definitely use the array to get the result I want.

2 Likes

Hi, where did you find this

      '?callback=?' +
      '&action=opensearch' +
      '&format=json' +
      '&profile=fuzzy' +
      '&limit=10' +
      '&prop=imageinfo&format=json&iiprop=urll' +
      '&search=' ``` ??? I searched for this in the docuements for almost 1-2 hours.
1 Like

I don’t understand why the URL Kevin wrote works and this URL doesn’t https://en.wikipedia.org/w/api.php?format=json&action=opensearch&limit=10&search=Something help pls

How does yours not work?

When I plug that into my browser address bar, I get the following response:

[
  "Something",
  [
    "Something",
    "Something (Beatles song)",
    "Something Just Like This",
    "Something Wicked This Way Comes (novel)",
    "Something for Kate",
    "Something/Anything?",
    "Something Awful",
    "Something to Remember",
    "Something Rotten!",
    "Something Corporate"
  ],
  [
    "Something may refer to:",
    "\"Something\" is a song by the Beatles, written by George Harrison and released on the band's 1969 album Abbey Road.",
    "\"Something Just Like This\" is a song by American electronic music duo The Chainsmokers and British rock band Coldplay.",
    "Something Wicked This Way Comes is a 1962 dark fantasy novel by Ray Bradbury. It is about 13-year-old best friends, Jim Nightshade and William Halloway, and their nightmarish experience with a traveling carnival that comes to their Midwestern town one October, and how the boys learn about combating fear.",
    "Something for Kate (also seen as SFK) are an Australian alternative rock band, which formed in 1994 with Paul Dempsey on lead vocals and guitar, and Clint Hyndman on drums.",
    "Something/Anything? is a double album by Todd Rundgren, released in February 1972. It was Rundgren's third solo release, and was recorded in late 1971 in Los Angeles, New York City and Bearsville Studios, Woodstock.",
    "Something Awful, often abbreviated to SA, is a comedy website housing a variety of content, including blog entries, forums, feature articles, digitally edited pictures, and humorous media reviews.",
    "Something to Remember is a compilation album by American recording artist Madonna, released by Maverick and Warner Bros.",
    "Something Rotten! is an original musical comedy with a book by John O'Farrell and Karey Kirkpatrick and music and lyrics by Karey and Wayne Kirkpatrick.",
    "Something Corporate (also known as SoCo) was an American rock band from Orange County, California, formed in 1998. Their last line-up included vocalist and pianist Andrew McMahon, guitarists Josh Partington and Bobby Anderson, bassist Kevin Page and drummer Brian Ireland."
  ],
  [
    "https://en.wikipedia.org/wiki/Something",
    "https://en.wikipedia.org/wiki/Something_(Beatles_song)",
    "https://en.wikipedia.org/wiki/Something_Just_Like_This",
    "https://en.wikipedia.org/wiki/Something_Wicked_This_Way_Comes_(novel)",
    "https://en.wikipedia.org/wiki/Something_for_Kate",
    "https://en.wikipedia.org/wiki/Something/Anything%3F",
    "https://en.wikipedia.org/wiki/Something_Awful",
    "https://en.wikipedia.org/wiki/Something_to_Remember",
    "https://en.wikipedia.org/wiki/Something_Rotten!",
    "https://en.wikipedia.org/wiki/Something_Corporate"
  ]
]

That looks like a good response to me. If you can’t get that from your code, then the problem is probably in your code. Is it a CORS problem? Is there a problem with your ajax call? If you provide a link to your pen or a code sample, we can help more.