-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfetch-api-post-json.html
More file actions
107 lines (104 loc) · 3.11 KB
/
fetch-api-post-json.html
File metadata and controls
107 lines (104 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google Fonts -->
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
/>
<!-- CSS Reset -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css"
/>
<!-- Milligram CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css"
/>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fetch Api Post</title>
<style>
h1 {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="column">
<h1>How to send a post request using fetch</h1>
</div>
</div>
<div class="row">
<div class="column">
<form>
<fieldset>
<label for="nameField">Name</label>
<input
name="name"
type="text"
placeholder="CJ Patoilo"
id="nameField"
/>
<label for="ageRangeField">Age Range</label>
<select name="age" id="ageRangeField">
<option value="0-13">0-13</option>
<option value="14-17">14-17</option>
<option value="18-23">18-23</option>
<option value="24+">24+</option>
</select>
<label for="commentField">Comment</label>
<textarea
placeholder="Hi CJ …"
name="comment"
id="commentField"
></textarea>
<div class="float-right">
<input
name="sendToSelf"
value="1"
type="checkbox"
id="confirmField"
/>
<label class="label-inline" for="confirmField"
>Send a copy to yourself</label
>
</div>
<input class="button-primary" type="submit" value="Send" />
</fieldset>
</form>
</div>
</div>
</div>
<script>
const url = "https://hookb.in/6Jpom3WKwquLbb031X6E";
const formEl = document.querySelector("form");
formEl.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(formEl);
const formDataSerialized = Object.fromEntries(formData);
const jsonObject = {
...formDataSerialized,
sendToSelf: formDataSerialized.sendToSelf ? true : false,
};
try {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(jsonObject),
headers: {
"Content-Type": "application/json",
},
});
const json = await response.json();
console.log(json);
} catch (e) {
console.error(e);
alert("there as an error");
}
});
</script>
</body>
</html>