Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +22 -14
static/script.js
CHANGED
|
@@ -17,30 +17,38 @@ form.addEventListener("submit", async (e) => {
|
|
| 17 |
formData.append("query", question);
|
| 18 |
|
| 19 |
try {
|
| 20 |
-
const
|
| 21 |
method: "POST",
|
| 22 |
-
|
|
|
|
| 23 |
});
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
-
const data =
|
| 30 |
const answer = data.answer;
|
| 31 |
|
| 32 |
-
//
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
|
|
|
| 41 |
input.value = "";
|
|
|
|
| 42 |
} catch (err) {
|
| 43 |
-
|
|
|
|
| 44 |
}
|
| 45 |
});
|
| 46 |
|
|
|
|
| 17 |
formData.append("query", question);
|
| 18 |
|
| 19 |
try {
|
| 20 |
+
const res = await fetch("/ask", {
|
| 21 |
method: "POST",
|
| 22 |
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
| 23 |
+
body: new URLSearchParams({ query: question })
|
| 24 |
});
|
| 25 |
|
| 26 |
+
const text = await res.text();
|
| 27 |
+
console.log("RAW RESPONSE:", text);
|
| 28 |
+
|
| 29 |
+
if (!res.ok) {
|
| 30 |
+
currentAnswer.textContent = `Error ${res.status}: ${text}`;
|
| 31 |
+
return;
|
| 32 |
}
|
| 33 |
|
| 34 |
+
const data = JSON.parse(text);
|
| 35 |
const answer = data.answer;
|
| 36 |
|
| 37 |
+
// Move to history
|
| 38 |
+
const item = document.createElement("div");
|
| 39 |
+
item.className = "history-item";
|
| 40 |
+
item.innerHTML = `
|
| 41 |
+
<div class="history-question">${question}</div>
|
| 42 |
+
<div class="history-answer">${answer}</div>
|
| 43 |
+
`;
|
| 44 |
+
historyList.prepend(item);
|
| 45 |
|
| 46 |
+
currentAnswer.style.display = "none";
|
| 47 |
input.value = "";
|
| 48 |
+
|
| 49 |
} catch (err) {
|
| 50 |
+
console.error(err);
|
| 51 |
+
currentAnswer.textContent = "JS error: " + err.message;
|
| 52 |
}
|
| 53 |
});
|
| 54 |
|