Skip to content

Commit 1ff2bc1

Browse files
committed
updates script
1 parent 6f44e49 commit 1ff2bc1

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

API-HTTP_POST_method/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>HTTP POST METHOD</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<h1>JSON</h1>
11+
<h1>JavaScript Object Notation</h1>
12+
<h2>Dynamically Send data to server:</h2>
13+
14+
<input id="title" type="text" placeholder="title">
15+
<br>
16+
<input id="bodyContent" type="text" placeholder="body part">
17+
<br>
18+
<button id="submit">submit</button>
19+
<p>note: after submitting use inspect to the result</p>
20+
21+
<script src="script.js"></script>
22+
</body>
23+
</html>

API-HTTP_POST_method/script.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
document.getElementById("submit").addEventListener("click", function(){
3+
const title = document.getElementById("title").value;
4+
const bodyContent = document.getElementById("bodyContent").value;
5+
6+
const post = {
7+
title: title,
8+
body: bodyContent
9+
};
10+
postToServer(post)
11+
})
12+
13+
function postToServer(post){
14+
fetch('https://jsonplaceholder.typicode.com/posts', {
15+
method: 'POST',
16+
body: JSON.stringify(post),
17+
headers: {
18+
"Content-type": "application/json; charset=UTF-8"
19+
}
20+
})
21+
.then(response => response.json())
22+
.then(json => console.log(json))
23+
}

API-HTTP_POST_method/style.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
body{
2+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
3+
color: #0F141E;
4+
text-align: center;
5+
background: aliceblue;
6+
}
7+
h1{
8+
color: #080e50;
9+
}
10+
p{
11+
color: red;
12+
}
13+
input{
14+
padding: 10px 10px;
15+
margin-bottom: 20px;
16+
border: 2px solid blue;
17+
border-radius: 5px;
18+
font-size: 20px;
19+
}
20+
#submit{
21+
padding: 10px 20px;
22+
font-size: 18px;
23+
cursor: pointer;
24+
border: 2px solid blue;
25+
font-weight: bold;
26+
border-radius: 5px;
27+
}

0 commit comments

Comments
 (0)