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

118
Visualizações
Javascript AJAX How to Fetch Data from API by ID

I'm trying to create a program that allows a user to input a number and fetches the movie data from an API by accessing the id index, so for example if I wanted to access the 3rd ID of the API, it would fetch all the elements from "https://my-json-server.typicode.com/daijonbereola/restful-api-react/movies/3". So far, no movie has been found and am not sure what is wrong with my Javascript. Here it is pasted below:

<form>
    <input type="number" id="inputId" placeholder="Enter a number"/>
    <input type="button" id="fetchId" value="Fetch Movie" class="btn btn-primary"/>
</form>
    <script>
        let id = document.getElementById("inputId").value;
        
        let fetchId = document.getElementById("fetchId");
        fetchId.addEventListener("click", selectHandler);
        
        
        function selectHandler() {

            // Instantiate an new XHR Object
            const xhr = new XMLHttpRequest();

            // Open an obejct (GET/POST, PATH,
            // ASYN-TRUE/FALSE)
            xhr.open("GET",
`https://my-json-server.typicode.com/daijonbereola/restful-api-react/movies/${id}`, true);

            // When response is ready
            xhr.onload = function () {
                if (this.status === 200) {

                    // Changing string data into JSON Object
                    obj = JSON.parse(this.responseText);

                    // Getting the element
                    let list = document.getElementById("list");
                    id = obj.id;
                    console.log(id);
                    if(id){
                    str = ""
                        str +=
                            `
                            <div style="padding: 5px 5px;">
                                <a href=${obj.url}>${obj.title}</a>
                                <image style="max-width: 60%;"src=${obj.image} alt = "movie poster"/>
                            </div>
                            `;
                        list.innerHTML = str;
                    } else{
                        str = ""
                        str += `<p>Movie not found...Please try again</p>`;
                        list.innerHTML = str;
                    }
                }
                else {
                    console.log("File not found");
                }
            }
            xhr.send();
        }
    </script>
about 4 years ago · Juan Pablo Isaza
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