How to retrieve data from Wikipedia API

Hello everyone,
I’m currently working to finish the Front-End intermediate level project “Build a Wikipedia Viewer”. I have faced a problem, which I am trying to find a solution for days. That’s why I am asking your help.

Here it is:
When the user searches for a specific term, the server retrieves information from the following wikipedia API query:
https://en.wikipedia.org/w/api.php?action=opensearch&format=json&redirects=return&search=” + term

The javascript code snippet is the following:

function retrieve(keyword)
{
    var JSONSite = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&redirects=return&search=";
    JSONSite += keyword;

    alert("This is executed normally.");

    $.getJSON("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&redirects=return&search=term",  function (data)
    {
        alert("This is not executed.");
    });

So, the problem is that the code inside the .getJSON function is not executing. What’s wrong with the code? Am I missing something?

Have you got a CodePen? I tried copying the URL to the browser and it worked fine. If using CodePen, make sure you’re connected via HTTPS or you’ll hit a CORS restriction.

1 Like

Have a look at this example

Is that because of the “term” in the string you pass to $.getJSON?