diff --git a/gui/main.js b/gui/main.js index 34a4962..57bfe5b 100644 --- a/gui/main.js +++ b/gui/main.js @@ -27,11 +27,12 @@ document.addEventListener("DOMContentLoaded", function () { .then(r => r.json()) .then(quiz => { // console.log(quiz) + // console.log(quiz.questions) var questions = quiz.questions var questions_html = "
" questions.forEach(q => { - console.log(q) + // console.log(q) if (q.options) { let options_div = "" q.options.forEach(o => { @@ -68,16 +69,21 @@ document.addEventListener("DOMContentLoaded", function () { button.onclick = function () { const form = document.getElementById('form'); const formData = new FormData(form); - console.log(formData) for (const [key, value] of formData) { - console.log(quiz) - console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier? + // console.log(quiz) + // console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier? quiz.questions.find(q => q.id == key).student_answer = value // console.log(quiz) } - fetch('http://localhost:8000/save_student_answers?' + new URLSearchParams({ - student_answers: JSON.stringify(quiz) - })) + fetch('http://localhost:8000/save_student_answers', { + method: 'POST', + // mode: 'no-cors', + // headers: { + // 'Accept': 'text/plain', + // 'Content-Type': 'text/plain' + // }, + body: JSON.stringify(quiz) + }) document.getElementById("main").innerHTML = "

Тестирование окончено

" }; // where do we want to have the button to appear? diff --git a/main.py b/main.py index 5e9188c..742a053 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI +from fastapi import FastAPI, Body from fastapi.middleware.cors import CORSMiddleware # CORS from fastapi.staticfiles import StaticFiles import json @@ -78,8 +78,8 @@ def get_quiz(student_id, student: str): "questions": questions_for_student } -@app.get("/save_student_answers") -def send_student_answers(student_answers: str): +@app.post("/save_student_answers") +def send_student_answers(student_answers: str = Body()): """Save answers as-is and check answers Args: