Skip to content

Commit 53bce5b

Browse files
committed
api-basics
0 parents  commit 53bce5b

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

README.md

38 Bytes

API-javascript

index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>API-javascript</title>
9+
</head>
10+
11+
<body>
12+
<h1 style="text-align: center;">Javascript API</h1>
13+
14+
<ul id="users">
15+
16+
</ul>
17+
18+
<button onclick="loadData()">Load Data</button>
19+
<button onclick="loadUsers()">Load Users</button>
20+
<button onclick="loadPosts()">Load Posts</button>
21+
22+
23+
<script src="js/app.js"></script>
24+
</body>
25+
26+
</html>

js/app.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function loadData() {
2+
fetch('https://jsonplaceholder.typicode.com/todos/1')
3+
.then(response => response.json())
4+
.then(json => console.log(json))
5+
}
6+
7+
function loadUsers() {
8+
fetch('https://jsonplaceholder.typicode.com/users')
9+
.then(response => response.json())
10+
.then(data => displayUser(data));
11+
}
12+
13+
function loadPosts() {
14+
fetch('https://jsonplaceholder.typicode.com/posts')
15+
.then(response => response.json())
16+
.then(json => console.log(json));
17+
}
18+
19+
20+
function displayUser(data) {
21+
const ul = document.getElementById('users');
22+
for (const users of data) {
23+
console.log(users.name);
24+
const li = document.createElement('li');
25+
li.innerText = `name: ${users.name}
26+
email: ${users.email}
27+
28+
`
29+
ul.appendChild(li);
30+
}
31+
32+
33+
}

js/post.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function loadPosts() {
2+
fetch('https://jsonplaceholder.typicode.com/posts')
3+
.then(response => response.json())
4+
.then(data => displayPosts(data));
5+
}
6+
7+
loadPosts();
8+
9+
function displayPosts(post) {
10+
console.log(post);
11+
const section = document.getElementById('section');
12+
for (const data of post) {
13+
const div = document.createElement('div');
14+
div.classList.add('design');
15+
div.innerHTML = `
16+
<h3>${data.title}</h3>
17+
<p>${data.body}</p>
18+
`
19+
section.appendChild(div);
20+
}
21+
}

js/stringify-parse.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const shop = {
2+
name: "Peom Dutta",
3+
address: "Chittagong",
4+
profit: 15000,
5+
owner: {
6+
names: "HBD",
7+
profession: "banker",
8+
id: 10
9+
},
10+
isExpensive: true,
11+
};
12+
13+
console.log(shop);
14+
15+
const shopStringified = JSON.stringify(shop);
16+
console.log(shopStringified);
17+
18+
const shopParsed = JSON.parse(shopStringified);
19+
console.log(shopParsed);
20+

post.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>post-javascript</title>
9+
10+
<style>
11+
.design {
12+
background-color: bisque;
13+
padding: 10px;
14+
margin: 10px;
15+
border: 1px solid rgb(204, 33, 33);
16+
border-radius: 10px;
17+
}
18+
</style>
19+
</head>
20+
21+
<body>
22+
<h1>Post Displaying</h1>
23+
24+
<section id="section">
25+
26+
</section>
27+
28+
<script src="js/post.js"></script>
29+
</body>
30+
31+
</html>

0 commit comments

Comments
 (0)