Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!