-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (29 loc) · 1.03 KB
/
Copy pathscript.js
File metadata and controls
33 lines (29 loc) · 1.03 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
const responseElement = document.getElementById('response');
const inputElement = document.getElementById('input');
const formElement = document.getElementById('form');
formElement.addEventListener('submit', event => {
event.preventDefault(); // prevent the page from reloading
fetch('http://localhost:3000/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: inputElement.value
})
})
.then(res => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json();
})
.then(res => {
responseElement.innerText = res[0]?.label === 'POSITIVE' ? 'Positive vibes detected' : 'Negative vibes detected!';
inputElement.value = '';
})
.catch(error => {
console.error('Error:', error); // Logging the error
responseElement.innerText = 'Failed to fetch data. Please try again later.';
});
});