Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Wikipedia API with JSONP

I've been trying for the last few days to make my code work, but I just can't find the problem.
I want to make communication with the Wikipedia server and get their JSON API so I can make a list of items corresponding to the input value of searchInput.
I've been looking into JSONP, finding in the end that I can add "&callback=?" to my API request and that it should work. Now, even though I've added it, the communication still isn't happening.
I've noticed that the console on codepen.io returns "untitled" for a moment while initializing the code after processing the "#searchInput" input. Perhaps the problem is in my for...in loop. Do you have any idea what I should do?
The link to my code: http://codepen.io/nedsalk/pen/zqbqgW?editors=1010
(JQuery is already enabled in the "settings" menu)
If you prefer the .html edition of the code:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Object Oriented JavaScript </title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js">   </script>
</head>

<body>
<h1> Wikipedia viewer </h1>
<a href="http://en.wikipedia.org/wiki/Special:Randompage" target="_blank">Go random! </a>
<form>
<input type="text" name="searchInput" id="searchInput" placeholder="Search Wikipedia" 
onkeydown = "if (event.keyCode == 13)
     document.getElementById('submit-button').click()"/>
<input type="submit" id="submit-button"/>
</form>
<div id="list"></div>
<script>
$(document).ready(function() {

    $("#submit-button").on("click",function (){
        var input=$("#searchInput").val();
        $.getJSON('https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=' + encodeURIComponent(input) + '&prop=extracts&exlimit=10&exintro&exsentences=2&format=json&callback=?',
            function(API){
                $("#list").empty();
                for (var id in API.query.pages)
                {if(API.query.pages.hasOwnProperty(id){
                    $("#list").html('<a target="_blank" href="http://en.wikipedia.org/?curid=' + id + '">'
                                    +'<div id="searchList">'
                                    + "<h2>" + id.title + "</h2>"
                                    + "<br>"
                                    + "<h3>" + id.extract + "</h3>"
                                    + "</div></a><br>")
                }}

            })
      })
})
</script>
</body>
</html>
like image 681
nedsalk Avatar asked Oct 18 '22 09:10

nedsalk


1 Answers

You have several issues in your code:

  • you should hook to the submit event of the form, not the click of the button, and use event.preventDefault() to stop the submission.
  • you loop through the keys of the returned object and attempt to access properties of those strings, instead of using the keys to access the underlying properties.
  • you set the html() in each loop, so only the final item will be visible. You should use append() instead.

Try this:

$("form").on("submit", function(e) {
    e.preventDefault();
    var input = $("#searchInput").val();
    $.getJSON('https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=' + encodeURIComponent(input) + '&prop=extracts&exlimit=10&exintro&exsentences=2&format=json&callback=?', function(response) {
        var pages = response.query.pages;
        $("#list").empty();

        for (var id in pages) {
            $("#list").append('<a target="_blank" href="http://en.wikipedia.org/?curid=' + id + '">' +
                '<div id="searchList">' +
                "<h2>" + pages[id].title + "</h2>" +
                "<br>" +
                "<h3>" + pages[id].extract + "</h3>" +
                "</div></a><br>")
        }
    });
});

Working example

like image 67
Rory McCrossan Avatar answered Oct 21 '22 02:10

Rory McCrossan