-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOST_API&PRINT_Data.html
More file actions
74 lines (68 loc) · 2.29 KB
/
Copy pathPOST_API&PRINT_Data.html
File metadata and controls
74 lines (68 loc) · 2.29 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>
<body>
<div class="container mt-2 py-2">
<h1>User Registration</h1>
<form id="registrationForm">
<label for="userName">Username:</label>
<input id="userName" /><br />
<br />
<label for="email">Email:</label>
<input id="email" type="email" /><br />
<br />
<label for="password">Password:</label>
<input id="password" type="password" /><br />
<br />
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
<div id="responseContainer" class="container mt-2"></div>
<script>
const registrationForm = document.querySelector('#registrationForm');
const responseContainer = document.querySelector('#responseContainer');
const userName = document.querySelector('#userName');
const email = document.querySelector('#email');
const password = document.querySelector('#password');
registrationForm.addEventListener('submit', function (event) {
event.preventDefault();
const apiUrl = 'https://jsonplaceholder.typicode.com/users';
const requestData = {
userName: userName.value,
email: email.value,
password: password.value,
};
fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(requestData),
headers: {
'Content-Type': 'application/json',
},
})
.then(function (response) {
return response.json();
})
.then(function (data) {
if (data) {
responseContainer.textContent = JSON.stringify(data);
} else {
responseContainer.textContent =
'Failed to make the User Registration POST call.';
}
})
.catch(function (error) {
responseContainer.textContent =
'An error occured while making the User Registration POST call.';
});
});
</script>
</body>
</html>