Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I thought of using the Fetch Api as an upgrade from the traditional w… #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions ghibli/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ container.setAttribute('class', 'container');

app.appendChild(logo);
app.appendChild(container);

/*
var request = new XMLHttpRequest();
request.open('GET', 'https://ghibliapi.herokuapp.com/films', true);
request.onload = function () {
Expand Down Expand Up @@ -38,4 +38,37 @@ request.onload = function () {
}
}

request.send();
request.send();
*/
//using the fetch api to send our request returns promise
fetch(' https://ghibliapi.herokuapp.com/films')
.then((response) => {
//returning it in json format
return response.json()
})
.then((data)=>{
data.forEach((movie) => {
//display our data from the api to the DOM
// Create a div with a card class
const card = document.createElement('div')
card.setAttribute('class', 'card')

// Create an h1 and set the text content to the film's title
const h1 = document.createElement('h1')
h1.textContent = movie.title

// Create a p and set the text content to the film's description
const p = document.createElement('p')
movie.description = movie.description.substring(0, 300) // Limit to 300 chars
p.textContent = `${movie.description}...` // End with an ellipses

// Append the cards to the container element
container.appendChild(card)

// Each card will contain an h1 and a p
card.appendChild(h1)
card.appendChild(p)
})
}).catch((err)=>{
console.log('error')
})