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

179
Visualizações
What am I doing wrong when trying to pull Data from this API and populate a select dropdown?

I am trying to pull the contents of the ASV bible off the api at biblia.api and populate my select dropdown with the data, however I am doing something wrong as the data is not populating my select dropdown. However, I know the endpoint is correct and it does load up the json object with the bible contents in my console.

$(document).ready(function(){
  let dropdown = document.getElementById('books');
dropdown.length = 0;

let defaultOption = document.createElement('option');
defaultOption.text = 'Choose Book of the Bible';

dropdown.add(defaultOption);
dropdown.selectedIndex = 0;

const url = 'https://api.biblia.com/v1/bible/contents/ASV?key=aa43781d7b806b8d6764191dce895e10';

const request = new XMLHttpRequest();
request.open('GET', url, true);

request.onload = function() {
  if (request.status === 200) {
    const books = JSON.parse(request.responseText);
    let option;
    for (let i = 0; i < books.length; i++) {
      option = document.createElement('option');
      option.text = books[i].passage;
      option.value = books[i].passage;
      dropdown.add(option);
        console.log(books[i].passage);
    }
   } else {
    // Reached the server, but it returned an error
  }   
}

request.onerror = function() {
  console.error('An error occurred fetching the JSON from ' + url);
};

request.send();
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Consider the following.

$(function() {
  function loadSelect(target, url) {
    var dropdown = $(target);
    var defaultOption = $('<option>').html("Choose Book of the Bible").appendTo(dropdown);
    $.getJSON(url, function(results) {
      $.each(results.books, function(i, book) {
        $("<option>", {
          value: i
        }).html(book.passage).appendTo(dropdown);
      });
    });
  }

  loadSelect("#books", "https://api.biblia.com/v1/bible/contents/ASV?key=aa43781d7b806b8d6764191dce895e10");
});

It is considered a better practice to use all JavaScript or jQuery and not to mix them. You might like to use $.getJSON().

about 4 years ago · Juan Pablo Isaza Relatório

0

Your problem is that you are iterating over "responseText", which contains only one item named books.

To do what you try to achieve, you should use something like that in your request.onload function :

const data = JSON.parse(request.responseText);
const books = data.books; 
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