From 4f4d9af67dba4c500dbae2f584110bb945421a77 Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Wed, 17 Jul 2024 14:29:38 +0200 Subject: [PATCH] =?UTF-8?q?Bytt=20tillbaka=20fr=C3=A5n=20XMLHttpRequest()?= =?UTF-8?q?=20till=20fetch()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smartassist/src/frontend.js | 98 ++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/smartassist/src/frontend.js b/smartassist/src/frontend.js index 0b80540..f64956d 100644 --- a/smartassist/src/frontend.js +++ b/smartassist/src/frontend.js @@ -4,62 +4,62 @@ const chatbox = document.getElementById('chatbox'); const userInput = document.getElementById('userInput'); // Define a function to send the user's message to the AI -// function sendMessage() { -// // Get the user's input message and trim any whitespace -// const message = userInput.value.trim(); - -// // Check if the message is not empty -// if (message !== '') { -// // Send a POST request to the /api/chat endpoint with the message -// fetch('/api/chat', { -// method: 'POST', -// headers: { 'Content-Type': 'application/json' }, -// body: JSON.stringify({ message }), -// }) -// .then(response => response.json()) -// .then(data => { -// // Get the AI's response from the API data -// const aiResponse = data.response; - -// // Render the user's original message in the chatbox -// renderMessage(message, 'user-message'); - -// // Render the AI's response in the chatbox -// renderMessage(aiResponse, 'ai-response'); - -// // Clear the user input field for the next message -// userInput.value = ''; -// }) -// .catch(error => console.error('Error sending message:', error)); -// } -// } - function sendMessage() { - const message = userInput.value.trim(); - if (message !== '') { - // Create a new XMLHttpRequest object - const xhr = new XMLHttpRequest(); + // Get the user's input message and trim any whitespace + const message = userInput.value.trim(); - // Configure the request - xhr.open('POST', '/api/chat'); // Set method to POST and URL - xhr.setRequestHeader('Content-Type', 'application/json'); // Essential for sending JSON + // Check if the message is not empty + if (message !== '') { + // Send a POST request to the /api/chat endpoint with the message + fetch('http://localhost:5000/api/chat', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ message }), + }) + .then(response => response.json()) + .then(data => { + // Get the AI's response from the API data + const aiResponse = data.response; - // Handle response data - xhr.onload = function() { - if (xhr.status >= 200 && xhr.status < 300) { // Successful response - const data = JSON.parse(xhr.responseText); - renderMessage(data.response, 'ai-response'); + // Render the user's original message in the chatbox + renderMessage(message, 'user-message'); - } else { - console.error('Request failed. Status:', xhr.status); - } - }; + // Render the AI's response in the chatbox + renderMessage(aiResponse, 'ai-response'); - // Send the request - xhr.send(JSON.stringify({ message })); - } + // Clear the user input field for the next message + userInput.value = ''; + }) + .catch(error => console.error('Error sending message:', error)); + } } +// function sendMessage() { +// const message = userInput.value.trim(); +// if (message !== '') { +// // Create a new XMLHttpRequest object +// const xhr = new XMLHttpRequest(); + +// // Configure the request +// xhr.open('POST', 'http://localhost:5000/api/chat'); // Set method to POST and URL +// xhr.setRequestHeader('Content-Type', 'application/json'); // Essential for sending JSON + +// // Handle response data +// xhr.onload = function() { +// if (xhr.status >= 200 && xhr.status < 300) { // Successful response +// const data = JSON.parse(xhr.responseText); +// renderMessage(data.response, 'ai-response'); + +// } else { +// console.error('Request failed. Status:', xhr.status); +// } +// }; + +// // Send the request +// xhr.send(JSON.stringify({ message })); +// } +// } + // Define a function to render a message in the chatbox with a specific class name function renderMessage(text, className) {