Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
How do I get the data from API using Javascript and Ajax?

When I write something in the input and then press search I want to get the title from movies that has that word in it, how do I do that? Im using the omdb API. For example if I search for "Batman" I want to get all the movies titles that has "Batman" in it. Now when I search Batman I only get one Batman movie.

document.getElementById("getForm").addEventListener("submit", (e) => {
    e.preventDefault();
    loadMovies(document.querySelector("input[name='query']").value);
  });
  
  function loadMovies(name) {
    var omdbAPI = new XMLHttpRequest();
    var omdbURL = `http://www.omdbapi.com/?t=${name.replace(" ", "%20")}&type=movie&apikey=`;
    omdbAPI.open("get", omdbURL, true);
  
    omdbAPI.onload = function(event) {
      event.preventDefault();
  
      if (this.status == 200) {
        var result = JSON.parse(this.responseText);
  
        console.log(result);
  
        var output = "";
          output +=
            '<div class="user">' +
            '<h3>Titel: ' + result.Title + '</h3>' +
            '<p>Year: ' + result.Year + '</p>' +
            '</div>';
        document.getElementById('result').innerHTML = output;
      } else {
        alert("No results");
      }
    }
  
    omdbAPI.send();
  }
<form action="" method="get" id="getForm">
  Movie: <input type="text" name="query">
  <button type="submit">Search</button>
</form>

<div id="result">
            
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You aren't getting any results because OMDB requires an API key and you aren't providing one. When sending a proper GET request you receive the following response: {"Response":"False","Error":"No API key provided."}.

You are also reloading the page by allowing the default form submission action to occur, instead use event.preventDefault() to cancel the page reload from occurring when form submission occurs, then calling your function. I modified your function to include a name parameter to search with. The below should accomplish what you want, but since you don't have an API key you will still not get any results. Go here, to get an API key.

document.getElementById("getForm").addEventListener("submit", (e) => {
  e.preventDefault();
  loadMovies(document.querySelector("input[name='query']").value);
});

function loadMovies(name) {
  var omdbAPI = new XMLHttpRequest();
  var omdbURL = `http://www.omdbapi.com/?t=${name.replace(" ", "%20")}&type=movie`;
  omdbAPI.open("get", omdbURL, true);

  omdbAPI.onload = function(event) {
    event.preventDefault();

    if (this.status == 200) {
      var result = JSON.parse(this.responseText);

      console.log(result);

      var output = "";
      for (var i in result) {
        output +=
          '<div class="user">' +
          '<h3>Titel: ' + result.Title + '</h3>' +
          '</div>';
      }
      document.getElementById('result').innerHTML = output;
    } else {
      alert("No results");
    }
  }

  omdbAPI.send();
}
<form action="" method="get" id="getForm">
  Movie: <input type="text" name="query">
  <button type="submit">Search</button>
</form>

<div id="result">

</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda