What’s wrong with my code?

Asked

Viewed 39 times

0

I’m trying to make a wikipedia viewer for the Freecodecamp front-end project. So I’m working with the Wikipedia API. I’ve done everything, but I can’t find what’s wrong with my code... because it’s not working?

My HTML code

    <!-- título, etc -->
<div class = "header">
  <h1>Wikipedia Viewer</h1>
  <h4> Projeto para Free Code Camp</h4>
  <!-- link pra página aleatória
   <a href="https://en.wikipedia.org/wiki/Special:Ramdom"></a> -->
</div>

<!-- barra de pesquisa -->
<div class = "container">
<input class="form-control" id="searchTerm" placeholder = "Digite aqui">
<button id = "getResult" type = "button" class = "btn btn-primary"> Vá </button>
</div>

<!-- resultados da pesquisa -->
  <div class="resultbox"
  <ul id="output">
  </ul>
</div>

My Javascript code:

    $(document).ready(function() {
  $("#getResult").click(function() {
    var searchTerm = $("#searchTerm").val();
    var url =
      "https://en.wikipedia.org/w/api.php?action=opensearch&search=" +
      searchTerm +
      "&format=json&callback=?";

    $.ajax({
      type: "GET",
      url: url,
      async: false,
      dataType: "json",
      success: function(data) {
        $("#output").html(" ");
        for (var i = 0; i < data[1].length; i++) {
          $("#output").prepend(
            "<li><a href= " +
              data[3][i] +
              ">" +
              data[1][i] +
              "</a><p>" +
              data[2][i] +
              "</p></li>"
          );
        }
      },
      error: function(errorMessage) {
        alert("Error");
      }
    });
  });
});

You can check my project here : https://codepen.io/leojunioyuri/pen/jGEVdL

Thanks in advance. Any help is needed.

  • What error does it present?

1 answer

2

Missing include jQuery library,

<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

Inserts this into the html file header.

https://codepen.io/AnthraxisBR/pen/PJwjgQ

Browser other questions tagged

You are not signed in. Login or sign up in order to post.