I tried a lot of answers here, but all answers I found, people are using jquery, ajax, react or things like that. I would like a answer with pure javascript (vanilla js).
const movieApiMovies = () => {
fetch(movieApi_url + "movies/")
.then(response => response.json())
.then(function (data) {
let result = `<h2> Movies I've watched! </h2>`;
data.forEach((movie) => {
const {id, name, year, note_imdb, genre, duration} = movie;
result +=
`<div>
<h5> Movie ID: ${id} </h5>
<ul>
<li>Movie name: ${name}</li>
<li>Movie year: ${year}</li>
<li>Movie note on IMDB: ${note_imdb}</li>
<li>Movie Genre: ${genre}</li>
<li>Movie duration: ${duration}</li>
</ul>
</div>`;
document.getElementById('movieResult').innerHTML = result;
})
})
};
I have a animation, it's on this Div element.
<div class="boxLoading"></div>
And I have a button that is going to call everything.
<div id="button1">
<button class="button" id="moviesfromapi"onclick="movieApiMovies ()">Display</button>
</div>
Step 1: Add a background with a spinner gif on top of the page, then remove them when everything is loaded. Step 2: Add a little script right after the opening body tag to start displaying the load/splash screen. Save this answer.
Approach: First make the necessary JavaScript file, HTML file and CSS file. Then store the API URL in a variable (here api_url). Define a async function (here getapi()) and pass api_url in that function. Define a constant response and store the fetched data by await fetch() method.
The Fetch API allows you to asynchronously request for a resource. Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.
add loader before call and remove once you receive results, as follows
const movieApiMovies = () => {
let loader = `<div class="boxLoading"></div>`;
document.getElementById('movieResult').innerHTML = loader;
fetch(movieApi_url + "movies/")
.then(response => response.json())
.then(function (data) {
let result = `<h2> Movies I've watched! </h2>`;
data.forEach((movie) => {
const {id, name, year, note_imdb, genre, duration} = movie;
result +=
`<div>
<h5> Movie ID: ${id} </h5>
<ul>
<li>Movie name: ${name}</li>
<li>Movie year: ${year}</li>
<li>Movie note on IMDB: ${note_imdb}</li>
<li>Movie Genre: ${genre}</li>
<li>Movie duration: ${duration}</li>
</ul>
</div>`;
document.getElementById('movieResult').innerHTML = result;
})
})
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With