-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
107 lines (91 loc) · 3 KB
/
index.html
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>
<meta charset="UTF-8">
<title>Test Mediant Ltd</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
</head>
<body>
<div class="container m-5">
<div class="mb-4" id="container">
This is the default test text
</div>
<div class="d-flex gap-3">
<button class="btn btn-success" id="get">
Click for GET request test
</button>
<button class="btn btn-primary" id="post">
Click for POST request test
</button>
<button class="btn btn-warning" id="patch">
Click for PATCH request test
</button>
<button class="btn btn-danger" id="destroy">
Click for DELETE request test
</button>
</div>
</div>
<script>
window.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('container');
//get request
const getButton = document.getElementById('get');
if (getButton && container) {
getButton.addEventListener('click', () => {
ajax('GET')
.then((response) => {
container.textContent = response.message;
})
})
}
//post request
const postButton = document.getElementById('post');
if (postButton && container) {
postButton.addEventListener('click', () => {
ajax('POST', {'name': 'Jane Doe'})
.then((response) => {
container.textContent = response.message;
})
})
}
//patch request
const patchButton = document.getElementById('patch');
if (patchButton && container) {
patchButton.addEventListener('click', () => {
ajax('PATCH', {'name': 'John The Doe'})
.then((response) => {
container.textContent = response.message;
})
})
}
//DELETE request
const destroyButton = document.getElementById('destroy');
if (destroyButton && container) {
destroyButton.addEventListener('click', () => {
ajax('DELETE')
.then((response) => {
container.textContent = response.message;
})
})
}
});
async function ajax(method, body) {
let params = {
method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}
if (body) {
params['body'] = JSON.stringify(body);
}
return await fetch('/api/v1/test', params)
.then((res) => res.json())
}
</script>
</body>
</html>