Not getting JSON output after pressing enter to search

Not getting JSON output after pressing enter to search
0.0 0

#1

Mind taking a look at this? I could really use a hand on this one. Thanks. https://codepen.io/KimberlyDuclos/pen/ZxJRyx


#2

Hi Kimberly,

Modify your form handler like in the section below. This will let you output to the console the entire data object returned from the wiki api call.

That will let you find the properties you need. E.g. the snippet is under:

data.query.search[_index_].snippet

Note that forms fire a “submit” event on Enter/Return, so you can switch from your keydown() to submit(). You will however need to use e.preventDefault() to prevent your page from reloading after the submit.

  $('#search').submit(function(e) {
    e.preventDefault()

    searchQuery = document.getElementById('textInput').value;
    $.getJSON("https://en.wikipedia.org/w/api.php?callback=?",
              {
      "action":"query",
      "format":"json",
      "srsearch":searchQuery,
      "list":"search"
    }).done(function(data) {
      console.log(JSON.stringify(data))
      // document.getElementById("title").value= JSON.stringify(data);
      // document.getElementById("snippet").value=search[0].snippet;
      // document.getElementById("snippet").value=data[3][1];
    });
  });

#3

Thanks! I got it fixed and a better understanding of what I’m doing on the project.