scrimba
Frontend Career Path
Working with APIs
URLs & REST
BlogSpace - Add new post to list of posts
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

BlogSpace - Add new post to list of posts
AboutCommentsNotes
BlogSpace - Add new post to list of posts
Expand for more info
index.js
run
preview
console
fetch("https://apis.scrimba.com/jsonplaceholder/posts")
.then(res => res.json())
.then(data => {
const postsArr = data.slice(0, 5)
let html = ""
for (let post of postsArr) {
html += `
<h3>${post.title}</h3>
<p>${post.body}</p>
<hr />
`
}
document.getElementById("blog-list").innerHTML = html
})

document.getElementById("new-post").addEventListener("submit", function(e) {
e.preventDefault()
const postTitle = document.getElementById("post-title").value
const postBody = document.getElementById("post-body").value
const data = {
title: postTitle,
body: postBody
}

const options = {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
}

fetch("https://apis.scrimba.com/jsonplaceholder/posts", options)
.then(res => res.json())
.then(data => {
console.log(data)
/**
* Challenge: Update the DOM with the new blog entry
*/
})
})
Console
{title:
"New title"
, body:
"New Body"
, id:
101
}
,
/index.html
-4:20