-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
38 lines (32 loc) · 1.25 KB
/
request.js
File metadata and controls
38 lines (32 loc) · 1.25 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
document.getElementById('sendBtn').addEventListener('click', async function() {
const userInput = document.getElementById('userInput').value;
const chatbox = document.getElementById('chatbox');
// Input validation
if (!userInput.trim()) {
alert('Please enter a message.');
return;
}
const userMessage = document.createElement('div');
userMessage.textContent = "You: " + userInput;
chatbox.appendChild(userMessage);
// Clear the input field
document.getElementById('userInput').value = '';
try {
const response = await fetch('http://127.0.0.1:5000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: userInput })
});
const data = await response.json();
const botMessage = document.createElement('div');
botMessage.textContent = "Bot: " + data.reply;
chatbox.appendChild(botMessage);
} catch (error) {
const errorMessage = document.createElement('div');
errorMessage.textContent = "Error: Could not connect to the server.";
chatbox.appendChild(errorMessage);
console.error('Error:', error);
}
});